xref: /minix3/lib/libpuffs/pnode.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1 /*	$NetBSD: pnode.c,v 1.10 2008/08/12 19:44:39 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Antti Kantee.  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 AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #if !defined(lint)
30 __RCSID("$NetBSD: pnode.c,v 1.10 2008/08/12 19:44:39 pooka Exp $");
31 #endif /* !lint */
32 
33 #include <minix/type.h>
34 #include <sys/types.h>
35 
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 
41 #include "puffs.h"
42 #include "puffs_priv.h"
43 #include "proto.h"
44 
45 /*
46  * Well, you're probably wondering why this isn't optimized.
47  * The reason is simple: my available time is not optimized for
48  * size ... so please be patient ;)
49  */
50 struct puffs_node *
51 puffs_pn_new(struct puffs_usermount *pu, void *privdata)
52 {
53 	struct puffs_node *pn;
54 
55 	pn = calloc(1, sizeof(struct puffs_node));
56 	if (pn == NULL)
57 		return NULL;
58 
59 	pn->pn_data = privdata;
60 	pn->pn_mnt = pu;
61 	puffs_vattr_null(&pn->pn_va);
62 
63 	LIST_INSERT_HEAD(&pu->pu_pnodelst, pn, pn_entries);
64 
65 	return pn;
66 }
67 
68 void
69 puffs_pn_remove(struct puffs_node *pn)
70 {
71 	struct puffs_usermount *pu = pn->pn_mnt;
72 	assert(pu != NULL);
73 
74 	LIST_REMOVE(pn, pn_entries);
75 	pn->pn_flags |= PUFFS_NODE_REMOVED;
76 	if (pn->pn_count != 0) {
77 		/* XXX FS removes this pn from the list to prevent further
78 		 * lookups from finding node after remove/rm/rename op.
79 		 * But VFS still uses it, i.e. pnode is still open, and
80 		 * will put it later. Keep it in separate list to do reclaim
81 		 * in fs_put().
82 		 */
83 		LIST_INSERT_HEAD(&pu->pu_pnode_removed_lst, pn, pn_entries);
84 	}
85 }
86 
87 void
88 puffs_pn_put(struct puffs_node *pn)
89 {
90 	struct puffs_usermount *pu = pn->pn_mnt;
91 
92 	pu->pu_pathfree(pu, &pn->pn_po);
93 	/* Removes either from pu_pnodelst or pu_pnode_removed_lst */
94 	LIST_REMOVE(pn, pn_entries);
95 	free(pn);
96 }
97 
98 /* walk list, rv can be used either to halt or to return a value
99  * XXX (MINIX note): if fn is 0, then arg is ino_t and we search
100  * node with ino_t. TODO: modify docs.
101  */
102 void *
103 puffs_pn_nodewalk(struct puffs_usermount *pu, puffs_nodewalk_fn fn, void *arg)
104 {
105 	struct puffs_node *pn_cur, *pn_next;
106 	void *rv;
107 
108 	pn_cur = LIST_FIRST(&pu->pu_pnodelst);
109 	while (pn_cur) {
110 		pn_next = LIST_NEXT(pn_cur, pn_entries);
111 		if (fn) {
112 			rv = fn(pu, pn_cur, arg);
113 			if (rv)
114 				return rv;
115 		} else {
116 			if (pn_cur->pn_va.va_fileid == *((ino_t*) arg))
117 				return pn_cur;
118 		}
119 		pn_cur = pn_next;
120 	}
121 
122 	return NULL;
123 }
124 
125 void*
126 puffs_pn_nodeprint(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
127 {
128 	/* If arg is specified, print only pnodes with inum (should be only one,
129 	 * otherwise - all.
130 	 */
131 	if (arg != NULL) {
132 		ino_t inum = *(ino_t*)arg;
133 		if (pn->pn_va.va_fileid != inum) {
134 			return NULL;
135 		}
136 	}
137 	lpuffs_debug(" ino %ld used %d %s\n", pn->pn_va.va_fileid, pn->pn_count,
138 				pn->pn_po.po_path);
139 	/* If arg specified, it should be the only one pnode to be printed,
140 	 * but we walk through the rest of list for debugging purposes.
141 	 */
142 	return NULL;
143 }
144 
145 void
146 puffs_pn_nodeprintall(struct puffs_usermount *pu)
147 {
148 	puffs_pn_nodewalk(pu, puffs_pn_nodeprint, NULL);
149 }
150 
151 struct vattr *
152 puffs_pn_getvap(struct puffs_node *pn)
153 {
154 
155 	return &pn->pn_va;
156 }
157 
158 void *
159 puffs_pn_getpriv(struct puffs_node *pn)
160 {
161 
162 	return pn->pn_data;
163 }
164 
165 void
166 puffs_pn_setpriv(struct puffs_node *pn, void *priv)
167 {
168 
169 	pn->pn_data = priv;
170 }
171 
172 struct puffs_pathobj *
173 puffs_pn_getpo(struct puffs_node *pn)
174 {
175 
176 	return &pn->pn_po;
177 }
178 
179 struct puffs_usermount *
180 puffs_pn_getmnt(struct puffs_node *pn)
181 {
182 
183 	return pn->pn_mnt;
184 }
185 
186 /* convenience / shortcut */
187 void *
188 puffs_pn_getmntspecific(struct puffs_node *pn)
189 {
190 
191 	return pn->pn_mnt->pu_privdata;
192 }
193 
194 /*
195  * newnode parameters
196  */
197 void
198 puffs_newinfo_setcookie(struct puffs_newinfo *pni, puffs_cookie_t cookie)
199 {
200 
201 	*pni->pni_cookie = cookie;
202 }
203 
204 void
205 puffs_newinfo_setvtype(struct puffs_newinfo *pni, enum vtype vt)
206 {
207 
208 	*pni->pni_vtype = vt;
209 }
210 
211 void
212 puffs_newinfo_setsize(struct puffs_newinfo *pni, voff_t size)
213 {
214 
215 	*pni->pni_size = size;
216 }
217 
218 void
219 puffs_newinfo_setrdev(struct puffs_newinfo *pni, dev_t rdev)
220 {
221 
222 	*pni->pni_rdev = rdev;
223 }
224