summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/pathname.c
blob: 323489d67eac98352a816cb91f1fa8e5f7d36595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * University Copyright- Copyright (c) 1982, 1986, 1988
 * The Regents of the University of California
 * All Rights Reserved
 *
 * University Acknowledgment- Portions of this document are derived from
 * software developed by the University of California, Berkeley, and its
 * contributors.
 */


#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/uio.h>
#include <sys/errno.h>
#include <sys/pathname.h>
#include <sys/kmem.h>
#include <sys/cred.h>
#include <sys/vnode.h>
#include <sys/debug.h>

/*
 * Pathname utilities.
 *
 * In translating file names we copy each argument file
 * name into a pathname structure where we operate on it.
 * Each pathname structure can hold "pn_bufsize" characters
 * including a terminating null, and operations here support
 * allocating and freeing pathname structures, fetching
 * strings from user space, getting the next character from
 * a pathname, combining two pathnames (used in symbolic
 * link processing), and peeling off the first component
 * of a pathname.
 */

/*
 * Allocate contents of pathname structure.  Structure is typically
 * an automatic variable in calling routine for convenience.
 *
 * May sleep in the call to kmem_alloc() and so must not be called
 * from interrupt level.
 */
void
pn_alloc(struct pathname *pnp)
{
	pn_alloc_sz(pnp, MAXPATHLEN);
}
void
pn_alloc_sz(struct pathname *pnp, size_t sz)
{
	pnp->pn_path = pnp->pn_buf = kmem_alloc(sz, KM_SLEEP);
	pnp->pn_pathlen = 0;
	pnp->pn_bufsize = sz;
}

/*
 * Free pathname resources.
 */
void
pn_free(struct pathname *pnp)
{
	/* pn_bufsize is usually MAXPATHLEN, but may not be */
	kmem_free(pnp->pn_buf, pnp->pn_bufsize);
	pnp->pn_path = pnp->pn_buf = NULL;
	pnp->pn_pathlen = pnp->pn_bufsize = 0;
}

/*
 * Pull a path name from user or kernel space.
 * Called from pn_get() after allocation of a MAXPATHLEN buffer.
 * Also called directly with a TYPICALMAXPATHLEN-size buffer
 * on the stack as a local optimization.
 */
int
pn_get_buf(char *str, enum uio_seg seg, struct pathname *pnp,
	void *buf, size_t bufsize)
{
	int error;

	pnp->pn_path = pnp->pn_buf = buf;
	pnp->pn_bufsize = bufsize;
	if (seg == UIO_USERSPACE)
		error = copyinstr(str, pnp->pn_path, bufsize, &pnp->pn_pathlen);
	else
		error = copystr(str, pnp->pn_path, bufsize, &pnp->pn_pathlen);
	if (error)
		return (error);
	pnp->pn_pathlen--;		/* don't count null byte */
	return (0);
}

/*
 * Pull a path name from user or kernel space.
 */
int
pn_get(char *str, enum uio_seg seg, struct pathname *pnp)
{
	int error;
	void *buf;

	buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
	if ((error = pn_get_buf(str, seg, pnp, buf, MAXPATHLEN)) != 0)
		pn_free(pnp);
	return (error);
}

/*
 * Set path name to argument string.  Storage has already been allocated
 * and pn_buf points to it.
 *
 * On error, all fields except pn_buf will be undefined.
 */
int
pn_set(struct pathname *pnp, char *path)
{
	int error;

	pnp->pn_path = pnp->pn_buf;
	error = copystr(path, pnp->pn_path, pnp->pn_bufsize, &pnp->pn_pathlen);
	pnp->pn_pathlen--;		/* don't count null byte */
	return (error);
}

/*
 * Combine two argument path names by putting the second argument
 * before the first in the first's buffer.  This isn't very general;
 * it is designed specifically for symbolic link processing.
 * This function copies the symlink in-place in the pathname.  This is to
 * ensure that vnode path caching remains correct.  At the point where this is
 * called (from lookuppnvp), we have called pn_getcomponent(), found it is a
 * symlink, and are now replacing the contents.  The complen parameter indicates
 * how much of the pathname to replace.  If the symlink is an absolute path,
 * then we overwrite the entire contents of the pathname.
 */
int
pn_insert(struct pathname *pnp, struct pathname *sympnp, size_t complen)
{

	if (*sympnp->pn_path == '/') {
		/*
		 * Full path, replace everything
		 */
		if (pnp->pn_pathlen + sympnp->pn_pathlen >= pnp->pn_bufsize)
			return (ENAMETOOLONG);
		if (pnp->pn_pathlen != 0)
			ovbcopy(pnp->pn_path, pnp->pn_buf + sympnp->pn_pathlen,
			    pnp->pn_pathlen);
		bcopy(sympnp->pn_path, pnp->pn_buf, sympnp->pn_pathlen);
		pnp->pn_pathlen += sympnp->pn_pathlen;
		pnp->pn_buf[pnp->pn_pathlen] = '\0';
		pnp->pn_path = pnp->pn_buf;
	} else {
		/*
		 * Partial path, replace only last component
		 */
		if ((pnp->pn_path - pnp->pn_buf) - complen +
		    pnp->pn_pathlen + sympnp->pn_pathlen >= pnp->pn_bufsize)
			return (ENAMETOOLONG);

		if (pnp->pn_pathlen != 0)
			ovbcopy(pnp->pn_path, pnp->pn_path - complen +
			    sympnp->pn_pathlen, pnp->pn_pathlen + 1);
		pnp->pn_path -= complen;
		bcopy(sympnp->pn_path, pnp->pn_path, sympnp->pn_pathlen);
		pnp->pn_pathlen += sympnp->pn_pathlen;
	}

	return (0);
}

int
pn_getsymlink(vnode_t *vp, struct pathname *pnp, cred_t *crp)
{
	struct iovec aiov;
	struct uio auio;
	int error;

	aiov.iov_base = pnp->pn_path = pnp->pn_buf;
	aiov.iov_len = pnp->pn_bufsize;
	auio.uio_iov = &aiov;
	auio.uio_iovcnt = 1;
	auio.uio_loffset = 0;
	auio.uio_segflg = UIO_SYSSPACE;
	auio.uio_extflg = UIO_COPY_CACHED;
	auio.uio_resid = pnp->pn_bufsize;
	if ((error = VOP_READLINK(vp, &auio, crp, NULL)) == 0) {
		pnp->pn_pathlen = pnp->pn_bufsize - auio.uio_resid;
		if (pnp->pn_pathlen == pnp->pn_bufsize)
			error = ENAMETOOLONG;
		else
			pnp->pn_path[pnp->pn_pathlen] = '\0';
	}
	return (error);
}

/*
 * Get next component from a path name and leave in
 * buffer "component" which should have room for
 * MAXNAMELEN bytes (including a null terminator character).
 */
int
pn_getcomponent(struct pathname *pnp, char *component)
{
	char c, *cp, *path, saved;
	size_t pathlen;

	path = pnp->pn_path;
	pathlen = pnp->pn_pathlen;
	if (pathlen >= MAXNAMELEN) {
		saved = path[MAXNAMELEN];
		path[MAXNAMELEN] = '/';	/* guarantees loop termination */
		for (cp = path; (c = *cp) != '/'; cp++)
			*component++ = c;
		path[MAXNAMELEN] = saved;
		if (cp - path == MAXNAMELEN)
			return (ENAMETOOLONG);
	} else {
		path[pathlen] = '/';	/* guarantees loop termination */
		for (cp = path; (c = *cp) != '/'; cp++)
			*component++ = c;
		path[pathlen] = '\0';
	}

	pnp->pn_path = cp;
	pnp->pn_pathlen = pathlen - (cp - path);
	*component = '\0';
	return (0);
}

/*
 * Skip over consecutive slashes in the path name.
 */
void
pn_skipslash(struct pathname *pnp)
{
	while (pnp->pn_pathlen > 0 && *pnp->pn_path == '/') {
		pnp->pn_path++;
		pnp->pn_pathlen--;
	}
}

/*
 * Sets pn_path to the last component in the pathname, updating
 * pn_pathlen.  If pathname is empty, or degenerate, leaves pn_path
 * pointing at NULL char.  The pathname is explicitly null-terminated
 * so that any trailing slashes are effectively removed.
 */
void
pn_setlast(struct pathname *pnp)
{
	char *buf = pnp->pn_buf;
	char *path = pnp->pn_path + pnp->pn_pathlen - 1;
	char *endpath;

	while (path > buf && *path == '/')
		--path;
	endpath = path + 1;
	while (path > buf && *path != '/')
		--path;
	if (*path == '/')
		path++;
	*endpath = '\0';
	pnp->pn_path = path;
	pnp->pn_pathlen = endpath - path;
}

/*
 * Eliminate any trailing slashes in the pathname.
 * Return non-zero iff there were any trailing slashes.
 */
int
pn_fixslash(struct pathname *pnp)
{
	char *start = pnp->pn_path;
	char *end = start + pnp->pn_pathlen;

	while (end > start && *(end - 1) == '/')
		end--;
	if (pnp->pn_pathlen == end - start)
		return (0);
	*end = '\0';
	pnp->pn_pathlen = end - start;
	return (1);
}

/*
 * Add a slash to the end of the pathname, if it will fit.
 * Return ENAMETOOLONG if it won't.
 */
int
pn_addslash(struct pathname *pnp)
{
	if (pnp->pn_path + pnp->pn_pathlen + 1 >=
	    pnp->pn_buf + pnp->pn_bufsize) {
		if (pnp->pn_pathlen + 1 >= pnp->pn_bufsize)	/* no room */
			return (ENAMETOOLONG);
		/*
		 * Move the component to the start of the buffer
		 * so we have room to add the trailing slash.
		 */
		ovbcopy(pnp->pn_path, pnp->pn_buf, pnp->pn_pathlen);
		pnp->pn_path = pnp->pn_buf;
	}
	pnp->pn_path[pnp->pn_pathlen++] = '/';
	pnp->pn_path[pnp->pn_pathlen] = '\0';
	return (0);
}