xref: /netbsd-src/lib/libperfuse/subr.c (revision 179b12252ecaf3553d9c2b7458ce62b6a2203d0c)
1 /*  $NetBSD: subr.c,v 1.2 2010/08/26 13:29:01 manu Exp $ */
2 
3 /*-
4  *  Copyright (c) 2010 Emmanuel Dreyfus. All rights reserved.
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions
8  *  are met:
9  *  1. Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  *  POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <err.h>
31 #include <errno.h>
32 #include <sysexits.h>
33 #include <syslog.h>
34 #include <puffs.h>
35 #include <paths.h>
36 
37 #include "perfuse_priv.h"
38 
39 struct puffs_node *
40 perfuse_new_pn(pu, parent)
41 	struct puffs_usermount *pu;
42 	struct puffs_node *parent;
43 {
44 	struct puffs_node *pn;
45 	struct perfuse_node_data *pnd;
46 
47 	if ((pnd = malloc(sizeof(*pnd))) == NULL)
48 		DERR(EX_OSERR, "malloc failed");
49 
50 	if ((pn = puffs_pn_new(pu, pnd)) == NULL)
51 		DERR(EX_SOFTWARE, "puffs_pn_new failed");
52 
53 	(void)memset(pnd, 0, sizeof(*pnd));
54 	TAILQ_INIT(&pnd->pnd_fh);
55 	pnd->pnd_ino = PERFUSE_UNKNOWN_INO;
56 	pnd->pnd_nlookup = 1;
57 	pnd->pnd_parent = parent;
58 	TAILQ_INIT(&pnd->pnd_pcq);
59 
60 	if (parent != NULL)
61 		PERFUSE_NODE_DATA(parent)->pnd_childcount++;
62 
63 	return pn;
64 }
65 
66 void
67 perfuse_destroy_pn(pn)
68 	struct puffs_node *pn;
69 {
70 	struct perfuse_node_data *pnd;
71 
72 	if ((pnd = puffs_pn_getpriv(pn)) != NULL) {
73 		if (pnd->pnd_parent != NULL)
74 			PERFUSE_NODE_DATA(pnd->pnd_parent)->pnd_childcount--;
75 
76 		if (pnd->pnd_dirent != NULL)
77 			free(pnd->pnd_dirent);
78 
79 		if (pnd->pnd_all_fd != NULL)
80 			free(pnd->pnd_all_fd);
81 #ifdef PERFUSE_DEBUG
82 		if (!TAILQ_EMPTY(&pnd->pnd_fh))
83 			DERRX(EX_SOFTWARE, "%s: non empty pnd_fh", __func__);
84 
85 		if (!TAILQ_EMPTY(&pnd->pnd_pcq))
86 			DERRX(EX_SOFTWARE, "%s: non empty pnd_pcq", __func__);
87 #endif /* PERFUSE_DEBUG */
88 
89 		free(pnd);
90 	}
91 
92 	puffs_pn_remove(pn);
93 
94 	return;
95 }
96 
97 
98 void
99 perfuse_new_fh(opc, fh)
100 	puffs_cookie_t opc;
101 	uint64_t fh;
102 {
103 	struct perfuse_node_data *pnd;
104 	struct perfuse_file_handle *pfh;
105 
106 	if (fh == FUSE_UNKNOWN_FH)
107 		return;
108 
109 	pnd = PERFUSE_NODE_DATA(opc);
110 	pnd->pnd_flags |= PND_OPEN;
111 
112 	if ((pfh = malloc(sizeof(*pfh))) == NULL)
113 		DERR(EX_OSERR, "malloc failed");
114 
115 	pfh->pfh_fh = fh;
116 
117 	TAILQ_INSERT_TAIL(&pnd->pnd_fh, pfh, pfh_entries);
118 
119 	return;
120 }
121 
122 void
123 perfuse_destroy_fh(opc, fh)
124 	puffs_cookie_t opc;
125 	uint64_t fh;
126 {
127 	struct perfuse_node_data *pnd;
128 	struct perfuse_file_handle *pfh;
129 
130 	pnd = PERFUSE_NODE_DATA(opc);
131 
132 	TAILQ_FOREACH(pfh, &pnd->pnd_fh, pfh_entries) {
133 		if (pfh->pfh_fh == fh) {
134 			TAILQ_REMOVE(&pnd->pnd_fh, pfh, pfh_entries);
135 			free(pfh);
136 			break;
137 		}
138 	}
139 
140 	if (TAILQ_EMPTY(&pnd->pnd_fh))
141 		pnd->pnd_flags &= ~PND_OPEN;
142 
143 	if (pfh == NULL)
144 		DERRX(EX_SOFTWARE, "%s: unexistant fh = %lld (double close?)",
145 		      __func__, fh);
146 
147 	return;
148 }
149 
150 uint64_t
151 perfuse_get_fh(opc)
152 	puffs_cookie_t opc;
153 {
154 	struct perfuse_node_data *pnd;
155 	struct perfuse_file_handle *pfh;
156 	uint64_t fh = FUSE_UNKNOWN_FH;
157 
158 	pnd = PERFUSE_NODE_DATA(opc);
159 
160 	if ((pfh = TAILQ_FIRST(&pnd->pnd_fh)) != NULL)
161 		fh = pfh->pfh_fh;;
162 
163 	return fh;
164 }
165 
166