xref: /freebsd/sys/contrib/openzfs/module/zfs/pathname.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
14  * Use is subject to license terms.
15  */
16 
17 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
18 /*	  All Rights Reserved  	*/
19 
20 /*
21  * University Copyright- Copyright (c) 1982, 1986, 1988
22  * The Regents of the University of California
23  * All Rights Reserved
24  *
25  * University Acknowledgment- Portions of this document are derived from
26  * software developed by the University of California, Berkeley, and its
27  * contributors.
28  */
29 
30 
31 #include <sys/types.h>
32 #include <sys/pathname.h>
33 #include <sys/kmem.h>
34 #include <sys/sysmacros.h>
35 
36 /*
37  * Pathname utilities.
38  *
39  * In translating file names we copy each argument file
40  * name into a pathname structure where we operate on it.
41  * Each pathname structure can hold "pn_bufsize" characters
42  * including a terminating null, and operations here support
43  * allocating and freeing pathname structures, fetching
44  * strings from user space, getting the next character from
45  * a pathname, combining two pathnames (used in symbolic
46  * link processing), and peeling off the first component
47  * of a pathname.
48  */
49 
50 /*
51  * Allocate contents of pathname structure.  Structure is typically
52  * an automatic variable in calling routine for convenience.
53  *
54  * May sleep in the call to kmem_alloc() and so must not be called
55  * from interrupt level.
56  */
57 void
pn_alloc(struct pathname * pnp)58 pn_alloc(struct pathname *pnp)
59 {
60 	pn_alloc_sz(pnp, MAXPATHLEN);
61 }
62 void
pn_alloc_sz(struct pathname * pnp,size_t sz)63 pn_alloc_sz(struct pathname *pnp, size_t sz)
64 {
65 	pnp->pn_buf = kmem_alloc(sz, KM_SLEEP);
66 	pnp->pn_bufsize = sz;
67 }
68 
69 /*
70  * Free pathname resources.
71  */
72 void
pn_free(struct pathname * pnp)73 pn_free(struct pathname *pnp)
74 {
75 	/* pn_bufsize is usually MAXPATHLEN, but may not be */
76 	kmem_free(pnp->pn_buf, pnp->pn_bufsize);
77 	pnp->pn_buf = NULL;
78 	pnp->pn_bufsize = 0;
79 }
80