tmpfs_rename.c revision 1.2.2.2 1 1.2.2.2 yamt /* $NetBSD: tmpfs_rename.c,v 1.2.2.2 2012/05/23 10:08:09 yamt Exp $ */
2 1.2.2.2 yamt
3 1.2.2.2 yamt /*-
4 1.2.2.2 yamt * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.2.2.2 yamt * All rights reserved.
6 1.2.2.2 yamt *
7 1.2.2.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 yamt * by Taylor R Campbell.
9 1.2.2.2 yamt *
10 1.2.2.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 yamt * modification, are permitted provided that the following conditions
12 1.2.2.2 yamt * are met:
13 1.2.2.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 yamt * documentation and/or other materials provided with the distribution.
18 1.2.2.2 yamt *
19 1.2.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2.2.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2.2.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2.2.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2.2.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.2.2.2 yamt */
31 1.2.2.2 yamt
32 1.2.2.2 yamt /*
33 1.2.2.2 yamt * tmpfs rename
34 1.2.2.2 yamt */
35 1.2.2.2 yamt
36 1.2.2.2 yamt #include <sys/cdefs.h>
37 1.2.2.2 yamt __KERNEL_RCSID(0, "$NetBSD: tmpfs_rename.c,v 1.2.2.2 2012/05/23 10:08:09 yamt Exp $");
38 1.2.2.2 yamt
39 1.2.2.2 yamt #include <sys/param.h>
40 1.2.2.2 yamt #include <sys/errno.h>
41 1.2.2.2 yamt #include <sys/kauth.h>
42 1.2.2.2 yamt #include <sys/mount.h>
43 1.2.2.2 yamt #include <sys/namei.h>
44 1.2.2.2 yamt #include <sys/stat.h>
45 1.2.2.2 yamt #include <sys/vnode.h>
46 1.2.2.2 yamt #include <sys/vnode_if.h>
47 1.2.2.2 yamt
48 1.2.2.2 yamt #include <miscfs/genfs/genfs.h>
49 1.2.2.2 yamt
50 1.2.2.2 yamt #include <fs/tmpfs/tmpfs_vnops.h>
51 1.2.2.2 yamt #include <fs/tmpfs/tmpfs.h>
52 1.2.2.2 yamt
53 1.2.2.2 yamt /*
54 1.2.2.2 yamt * Forward declarations
55 1.2.2.2 yamt */
56 1.2.2.2 yamt
57 1.2.2.2 yamt static int tmpfs_sane_rename(struct vnode *, struct componentname *,
58 1.2.2.2 yamt struct vnode *, struct componentname *,
59 1.2.2.2 yamt kauth_cred_t, bool);
60 1.2.2.2 yamt static bool tmpfs_rmdired_p(struct vnode *);
61 1.2.2.2 yamt static int tmpfs_gro_lock_directory(struct mount *, struct vnode *);
62 1.2.2.2 yamt
63 1.2.2.2 yamt static const struct genfs_rename_ops tmpfs_genfs_rename_ops;
64 1.2.2.2 yamt
65 1.2.2.2 yamt /*
66 1.2.2.2 yamt * tmpfs_sane_rename: The hairiest vop, with the saner API.
67 1.2.2.2 yamt *
68 1.2.2.2 yamt * Arguments:
69 1.2.2.2 yamt *
70 1.2.2.2 yamt * . fdvp (from directory vnode),
71 1.2.2.2 yamt * . fcnp (from component name),
72 1.2.2.2 yamt * . tdvp (to directory vnode),
73 1.2.2.2 yamt * . tcnp (to component name),
74 1.2.2.2 yamt * . cred (credentials structure), and
75 1.2.2.2 yamt * . posixly_correct (flag for behaviour if target & source link same file).
76 1.2.2.2 yamt *
77 1.2.2.2 yamt * fdvp and tdvp may be the same, and must be referenced and unlocked.
78 1.2.2.2 yamt */
79 1.2.2.2 yamt static int
80 1.2.2.2 yamt tmpfs_sane_rename(
81 1.2.2.2 yamt struct vnode *fdvp, struct componentname *fcnp,
82 1.2.2.2 yamt struct vnode *tdvp, struct componentname *tcnp,
83 1.2.2.2 yamt kauth_cred_t cred, bool posixly_correct)
84 1.2.2.2 yamt {
85 1.2.2.2 yamt struct tmpfs_dirent *fdirent, *tdirent;
86 1.2.2.2 yamt
87 1.2.2.2 yamt return genfs_sane_rename(&tmpfs_genfs_rename_ops,
88 1.2.2.2 yamt fdvp, fcnp, &fdirent, tdvp, tcnp, &tdirent,
89 1.2.2.2 yamt cred, posixly_correct);
90 1.2.2.2 yamt }
91 1.2.2.2 yamt
92 1.2.2.2 yamt /*
93 1.2.2.2 yamt * tmpfs_rename: The hairiest vop, with the insanest API. Defer to
94 1.2.2.2 yamt * genfs_insane_rename immediately.
95 1.2.2.2 yamt */
96 1.2.2.2 yamt int
97 1.2.2.2 yamt tmpfs_rename(void *v)
98 1.2.2.2 yamt {
99 1.2.2.2 yamt
100 1.2.2.2 yamt return genfs_insane_rename(v, &tmpfs_sane_rename);
101 1.2.2.2 yamt }
102 1.2.2.2 yamt
103 1.2.2.2 yamt /*
104 1.2.2.2 yamt * tmpfs_gro_directory_empty_p: Return true if the directory vp is
105 1.2.2.2 yamt * empty. dvp is its parent.
106 1.2.2.2 yamt *
107 1.2.2.2 yamt * vp and dvp must be locked and referenced.
108 1.2.2.2 yamt */
109 1.2.2.2 yamt static bool
110 1.2.2.2 yamt tmpfs_gro_directory_empty_p(struct mount *mp, kauth_cred_t cred,
111 1.2.2.2 yamt struct vnode *vp, struct vnode *dvp)
112 1.2.2.2 yamt {
113 1.2.2.2 yamt
114 1.2.2.2 yamt (void)mp;
115 1.2.2.2 yamt (void)cred;
116 1.2.2.2 yamt (void)dvp;
117 1.2.2.2 yamt KASSERT(mp != NULL);
118 1.2.2.2 yamt KASSERT(vp != NULL);
119 1.2.2.2 yamt KASSERT(dvp != NULL);
120 1.2.2.2 yamt KASSERT(vp != dvp);
121 1.2.2.2 yamt KASSERT(vp->v_mount == mp);
122 1.2.2.2 yamt KASSERT(dvp->v_mount == mp);
123 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
124 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
125 1.2.2.2 yamt
126 1.2.2.2 yamt return (VP_TO_TMPFS_NODE(vp)->tn_size == 0);
127 1.2.2.2 yamt }
128 1.2.2.2 yamt
129 1.2.2.2 yamt /*
130 1.2.2.2 yamt * tmpfs_gro_rename_check_possible: Check whether a rename is possible
131 1.2.2.2 yamt * independent of credentials.
132 1.2.2.2 yamt */
133 1.2.2.2 yamt static int
134 1.2.2.2 yamt tmpfs_gro_rename_check_possible(struct mount *mp,
135 1.2.2.2 yamt struct vnode *fdvp, struct vnode *fvp,
136 1.2.2.2 yamt struct vnode *tdvp, struct vnode *tvp)
137 1.2.2.2 yamt {
138 1.2.2.2 yamt
139 1.2.2.2 yamt (void)mp;
140 1.2.2.2 yamt KASSERT(mp != NULL);
141 1.2.2.2 yamt KASSERT(fdvp != NULL);
142 1.2.2.2 yamt KASSERT(fvp != NULL);
143 1.2.2.2 yamt KASSERT(tdvp != NULL);
144 1.2.2.2 yamt KASSERT(fdvp != fvp);
145 1.2.2.2 yamt KASSERT(fdvp != tvp);
146 1.2.2.2 yamt KASSERT(tdvp != fvp);
147 1.2.2.2 yamt KASSERT(tdvp != tvp);
148 1.2.2.2 yamt KASSERT(fvp != tvp);
149 1.2.2.2 yamt KASSERT(fdvp->v_type == VDIR);
150 1.2.2.2 yamt KASSERT(tdvp->v_type == VDIR);
151 1.2.2.2 yamt KASSERT(fdvp->v_mount == mp);
152 1.2.2.2 yamt KASSERT(fvp->v_mount == mp);
153 1.2.2.2 yamt KASSERT(tdvp->v_mount == mp);
154 1.2.2.2 yamt KASSERT((tvp == NULL) || (tvp->v_mount == mp));
155 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
156 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
157 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
158 1.2.2.2 yamt KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
159 1.2.2.2 yamt
160 1.2.2.2 yamt return genfs_ufslike_rename_check_possible(
161 1.2.2.2 yamt VP_TO_TMPFS_NODE(fdvp)->tn_flags, VP_TO_TMPFS_NODE(fvp)->tn_flags,
162 1.2.2.2 yamt VP_TO_TMPFS_NODE(tdvp)->tn_flags,
163 1.2.2.2 yamt (tvp? VP_TO_TMPFS_NODE(tvp)->tn_flags : 0), (tvp != NULL),
164 1.2.2.2 yamt IMMUTABLE, APPEND);
165 1.2.2.2 yamt }
166 1.2.2.2 yamt
167 1.2.2.2 yamt /*
168 1.2.2.2 yamt * tmpfs_gro_rename_check_permitted: Check whether a rename is
169 1.2.2.2 yamt * permitted given our credentials.
170 1.2.2.2 yamt */
171 1.2.2.2 yamt static int
172 1.2.2.2 yamt tmpfs_gro_rename_check_permitted(struct mount *mp, kauth_cred_t cred,
173 1.2.2.2 yamt struct vnode *fdvp, struct vnode *fvp,
174 1.2.2.2 yamt struct vnode *tdvp, struct vnode *tvp)
175 1.2.2.2 yamt {
176 1.2.2.2 yamt
177 1.2.2.2 yamt (void)mp;
178 1.2.2.2 yamt KASSERT(mp != NULL);
179 1.2.2.2 yamt KASSERT(fdvp != NULL);
180 1.2.2.2 yamt KASSERT(fvp != NULL);
181 1.2.2.2 yamt KASSERT(tdvp != NULL);
182 1.2.2.2 yamt KASSERT(fdvp != fvp);
183 1.2.2.2 yamt KASSERT(fdvp != tvp);
184 1.2.2.2 yamt KASSERT(tdvp != fvp);
185 1.2.2.2 yamt KASSERT(tdvp != tvp);
186 1.2.2.2 yamt KASSERT(fvp != tvp);
187 1.2.2.2 yamt KASSERT(fdvp->v_type == VDIR);
188 1.2.2.2 yamt KASSERT(tdvp->v_type == VDIR);
189 1.2.2.2 yamt KASSERT(fdvp->v_mount == mp);
190 1.2.2.2 yamt KASSERT(fvp->v_mount == mp);
191 1.2.2.2 yamt KASSERT(tdvp->v_mount == mp);
192 1.2.2.2 yamt KASSERT((tvp == NULL) || (tvp->v_mount == mp));
193 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
194 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
195 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
196 1.2.2.2 yamt KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
197 1.2.2.2 yamt
198 1.2.2.2 yamt return genfs_ufslike_rename_check_permitted(cred,
199 1.2.2.2 yamt fdvp, VP_TO_TMPFS_NODE(fdvp)->tn_mode,
200 1.2.2.2 yamt VP_TO_TMPFS_NODE(fdvp)->tn_uid,
201 1.2.2.2 yamt fvp, VP_TO_TMPFS_NODE(fvp)->tn_uid,
202 1.2.2.2 yamt tdvp, VP_TO_TMPFS_NODE(tdvp)->tn_mode,
203 1.2.2.2 yamt VP_TO_TMPFS_NODE(tdvp)->tn_uid,
204 1.2.2.2 yamt tvp, (tvp? VP_TO_TMPFS_NODE(tvp)->tn_uid : 0));
205 1.2.2.2 yamt }
206 1.2.2.2 yamt
207 1.2.2.2 yamt /*
208 1.2.2.2 yamt * tmpfs_gro_remove_check_possible: Check whether a remove is possible
209 1.2.2.2 yamt * independent of credentials.
210 1.2.2.2 yamt */
211 1.2.2.2 yamt static int
212 1.2.2.2 yamt tmpfs_gro_remove_check_possible(struct mount *mp,
213 1.2.2.2 yamt struct vnode *dvp, struct vnode *vp)
214 1.2.2.2 yamt {
215 1.2.2.2 yamt
216 1.2.2.2 yamt (void)mp;
217 1.2.2.2 yamt KASSERT(mp != NULL);
218 1.2.2.2 yamt KASSERT(dvp != NULL);
219 1.2.2.2 yamt KASSERT(vp != NULL);
220 1.2.2.2 yamt KASSERT(dvp != vp);
221 1.2.2.2 yamt KASSERT(dvp->v_type == VDIR);
222 1.2.2.2 yamt KASSERT(vp->v_type != VDIR);
223 1.2.2.2 yamt KASSERT(dvp->v_mount == mp);
224 1.2.2.2 yamt KASSERT(vp->v_mount == mp);
225 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
226 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
227 1.2.2.2 yamt
228 1.2.2.2 yamt return genfs_ufslike_remove_check_possible(
229 1.2.2.2 yamt VP_TO_TMPFS_NODE(dvp)->tn_flags, VP_TO_TMPFS_NODE(vp)->tn_flags,
230 1.2.2.2 yamt IMMUTABLE, APPEND);
231 1.2.2.2 yamt }
232 1.2.2.2 yamt
233 1.2.2.2 yamt /*
234 1.2.2.2 yamt * tmpfs_gro_remove_check_permitted: Check whether a remove is
235 1.2.2.2 yamt * permitted given our credentials.
236 1.2.2.2 yamt */
237 1.2.2.2 yamt static int
238 1.2.2.2 yamt tmpfs_gro_remove_check_permitted(struct mount *mp, kauth_cred_t cred,
239 1.2.2.2 yamt struct vnode *dvp, struct vnode *vp)
240 1.2.2.2 yamt {
241 1.2.2.2 yamt
242 1.2.2.2 yamt (void)mp;
243 1.2.2.2 yamt KASSERT(mp != NULL);
244 1.2.2.2 yamt KASSERT(dvp != NULL);
245 1.2.2.2 yamt KASSERT(vp != NULL);
246 1.2.2.2 yamt KASSERT(dvp != vp);
247 1.2.2.2 yamt KASSERT(dvp->v_type == VDIR);
248 1.2.2.2 yamt KASSERT(vp->v_type != VDIR);
249 1.2.2.2 yamt KASSERT(dvp->v_mount == mp);
250 1.2.2.2 yamt KASSERT(vp->v_mount == mp);
251 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
252 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
253 1.2.2.2 yamt
254 1.2.2.2 yamt return genfs_ufslike_remove_check_permitted(cred,
255 1.2.2.2 yamt dvp, VP_TO_TMPFS_NODE(dvp)->tn_mode, VP_TO_TMPFS_NODE(dvp)->tn_uid,
256 1.2.2.2 yamt vp, VP_TO_TMPFS_NODE(vp)->tn_uid);
257 1.2.2.2 yamt }
258 1.2.2.2 yamt
259 1.2.2.2 yamt /*
260 1.2.2.2 yamt * tmpfs_gro_rename: Actually perform the rename operation.
261 1.2.2.2 yamt */
262 1.2.2.2 yamt static int
263 1.2.2.2 yamt tmpfs_gro_rename(struct mount *mp, kauth_cred_t cred,
264 1.2.2.2 yamt struct vnode *fdvp, struct componentname *fcnp,
265 1.2.2.2 yamt void *fde, struct vnode *fvp,
266 1.2.2.2 yamt struct vnode *tdvp, struct componentname *tcnp,
267 1.2.2.2 yamt void *tde, struct vnode *tvp)
268 1.2.2.2 yamt {
269 1.2.2.2 yamt struct tmpfs_dirent **fdep = fde;
270 1.2.2.2 yamt struct tmpfs_dirent **tdep = tde;
271 1.2.2.2 yamt char *newname;
272 1.2.2.2 yamt
273 1.2.2.2 yamt (void)cred;
274 1.2.2.2 yamt KASSERT(mp != NULL);
275 1.2.2.2 yamt KASSERT(fdvp != NULL);
276 1.2.2.2 yamt KASSERT(fcnp != NULL);
277 1.2.2.2 yamt KASSERT(fdep != NULL);
278 1.2.2.2 yamt KASSERT(fvp != NULL);
279 1.2.2.2 yamt KASSERT(tdvp != NULL);
280 1.2.2.2 yamt KASSERT(tcnp != NULL);
281 1.2.2.2 yamt KASSERT(tdep != NULL);
282 1.2.2.2 yamt KASSERT(fdep != tdep);
283 1.2.2.2 yamt KASSERT((*fdep) != (*tdep));
284 1.2.2.2 yamt KASSERT((*fdep) != NULL);
285 1.2.2.2 yamt KASSERT((*fdep)->td_node == VP_TO_TMPFS_NODE(fvp));
286 1.2.2.2 yamt KASSERT((tvp == NULL) || ((*tdep) != NULL));
287 1.2.2.2 yamt KASSERT((tvp == NULL) || ((*tdep)->td_node == VP_TO_TMPFS_NODE(tvp)));
288 1.2.2.2 yamt KASSERT(fdvp != fvp);
289 1.2.2.2 yamt KASSERT(fdvp != tvp);
290 1.2.2.2 yamt KASSERT(tdvp != fvp);
291 1.2.2.2 yamt KASSERT(tdvp != tvp);
292 1.2.2.2 yamt KASSERT(fvp != tvp);
293 1.2.2.2 yamt KASSERT(fdvp->v_mount == mp);
294 1.2.2.2 yamt KASSERT(fvp->v_mount == mp);
295 1.2.2.2 yamt KASSERT(tdvp->v_mount == mp);
296 1.2.2.2 yamt KASSERT((tvp == NULL) || (tvp->v_mount == mp));
297 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
298 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
299 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
300 1.2.2.2 yamt KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
301 1.2.2.2 yamt
302 1.2.2.2 yamt if (tmpfs_strname_neqlen(fcnp, tcnp)) {
303 1.2.2.2 yamt newname = tmpfs_strname_alloc(VFS_TO_TMPFS(mp),
304 1.2.2.2 yamt tcnp->cn_namelen);
305 1.2.2.2 yamt if (newname == NULL)
306 1.2.2.2 yamt return ENOSPC;
307 1.2.2.2 yamt } else {
308 1.2.2.2 yamt newname = NULL;
309 1.2.2.2 yamt }
310 1.2.2.2 yamt
311 1.2.2.2 yamt /*
312 1.2.2.2 yamt * If we are moving from one directory to another, detach the
313 1.2.2.2 yamt * source entry and reattach it to the target directory.
314 1.2.2.2 yamt */
315 1.2.2.2 yamt if (fdvp != tdvp) {
316 1.2.2.2 yamt tmpfs_dir_detach(fdvp, *fdep);
317 1.2.2.2 yamt tmpfs_dir_attach(tdvp, *fdep, VP_TO_TMPFS_NODE(fvp));
318 1.2.2.2 yamt } else if (tvp == NULL) {
319 1.2.2.2 yamt /*
320 1.2.2.2 yamt * We are changing the directory. tmpfs_dir_attach and
321 1.2.2.2 yamt * tmpfs_dir_detach note the events for us, but for
322 1.2.2.2 yamt * this case we don't call them, so we must note the
323 1.2.2.2 yamt * event explicitly.
324 1.2.2.2 yamt */
325 1.2.2.2 yamt VN_KNOTE(fdvp, NOTE_WRITE);
326 1.2.2.2 yamt }
327 1.2.2.2 yamt
328 1.2.2.2 yamt /*
329 1.2.2.2 yamt * If we are replacing an existing target entry, delete it.
330 1.2.2.2 yamt *
331 1.2.2.2 yamt * XXX What if the target is a directory with whiteout entries?
332 1.2.2.2 yamt */
333 1.2.2.2 yamt if (tvp != NULL) {
334 1.2.2.2 yamt KASSERT((*tdep) != NULL);
335 1.2.2.2 yamt KASSERT((*tdep)->td_node == VP_TO_TMPFS_NODE(tvp));
336 1.2.2.2 yamt KASSERT((fvp->v_type == VDIR) == (tvp->v_type == VDIR));
337 1.2.2.2 yamt if (tvp->v_type == VDIR) {
338 1.2.2.2 yamt KASSERT(VP_TO_TMPFS_NODE(tvp)->tn_size == 0);
339 1.2.2.2 yamt KASSERT(VP_TO_TMPFS_NODE(tvp)->tn_links == 2);
340 1.2.2.2 yamt
341 1.2.2.2 yamt /*
342 1.2.2.2 yamt * Decrement the extra link count for `.' so
343 1.2.2.2 yamt * the vnode will be recycled when released.
344 1.2.2.2 yamt *
345 1.2.2.2 yamt * XXX Why not just release directory vnodes
346 1.2.2.2 yamt * when their link count is 1 instead of 0 in
347 1.2.2.2 yamt * tmpfs_inactive, since `.' is being treated
348 1.2.2.2 yamt * specially anyway?
349 1.2.2.2 yamt */
350 1.2.2.2 yamt VP_TO_TMPFS_NODE(tvp)->tn_links--;
351 1.2.2.2 yamt }
352 1.2.2.2 yamt tmpfs_dir_detach(tdvp, *tdep);
353 1.2.2.2 yamt tmpfs_free_dirent(VFS_TO_TMPFS(mp), *tdep);
354 1.2.2.2 yamt }
355 1.2.2.2 yamt
356 1.2.2.2 yamt /*
357 1.2.2.2 yamt * Update the directory entry's name if necessary, and flag
358 1.2.2.2 yamt * metadata updates. A memory allocation failure here is not
359 1.2.2.2 yamt * OK because we've already committed some changes that we
360 1.2.2.2 yamt * can't back out at this point, hence the early allocation
361 1.2.2.2 yamt * above.
362 1.2.2.2 yamt */
363 1.2.2.2 yamt if (newname != NULL) {
364 1.2.2.2 yamt KASSERT(tcnp->cn_namelen <= TMPFS_MAXNAMLEN);
365 1.2.2.2 yamt
366 1.2.2.2 yamt tmpfs_strname_free(VFS_TO_TMPFS(mp), (*fdep)->td_name,
367 1.2.2.2 yamt (*fdep)->td_namelen);
368 1.2.2.2 yamt (*fdep)->td_namelen = (uint16_t)tcnp->cn_namelen;
369 1.2.2.2 yamt (void)memcpy(newname, tcnp->cn_nameptr, tcnp->cn_namelen);
370 1.2.2.2 yamt (*fdep)->td_name = newname;
371 1.2.2.2 yamt
372 1.2.2.2 yamt VP_TO_TMPFS_NODE(fvp)->tn_status |= TMPFS_NODE_CHANGED;
373 1.2.2.2 yamt VP_TO_TMPFS_NODE(tdvp)->tn_status |= TMPFS_NODE_MODIFIED;
374 1.2.2.2 yamt }
375 1.2.2.2 yamt
376 1.2.2.2 yamt VN_KNOTE(fvp, NOTE_RENAME);
377 1.2.2.2 yamt
378 1.2.2.2 yamt #if 0 /* XXX */
379 1.2.2.2 yamt genfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
380 1.2.2.2 yamt #endif
381 1.2.2.2 yamt
382 1.2.2.2 yamt return 0;
383 1.2.2.2 yamt }
384 1.2.2.2 yamt
385 1.2.2.2 yamt /*
386 1.2.2.2 yamt * tmpfs_gro_remove: Rename an object over another link to itself,
387 1.2.2.2 yamt * effectively removing just the original link.
388 1.2.2.2 yamt */
389 1.2.2.2 yamt static int
390 1.2.2.2 yamt tmpfs_gro_remove(struct mount *mp, kauth_cred_t cred,
391 1.2.2.2 yamt struct vnode *dvp, struct componentname *cnp, void *de, struct vnode *vp)
392 1.2.2.2 yamt {
393 1.2.2.2 yamt struct tmpfs_dirent **dep = de;
394 1.2.2.2 yamt
395 1.2.2.2 yamt (void)vp;
396 1.2.2.2 yamt KASSERT(mp != NULL);
397 1.2.2.2 yamt KASSERT(dvp != NULL);
398 1.2.2.2 yamt KASSERT(cnp != NULL);
399 1.2.2.2 yamt KASSERT(dep != NULL);
400 1.2.2.2 yamt KASSERT(vp != NULL);
401 1.2.2.2 yamt KASSERT(dvp != vp);
402 1.2.2.2 yamt KASSERT(dvp->v_mount == mp);
403 1.2.2.2 yamt KASSERT(vp->v_mount == mp);
404 1.2.2.2 yamt KASSERT(dvp->v_type == VDIR);
405 1.2.2.2 yamt KASSERT(vp->v_type != VDIR);
406 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
407 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
408 1.2.2.2 yamt
409 1.2.2.2 yamt tmpfs_dir_detach(dvp, *dep);
410 1.2.2.2 yamt tmpfs_free_dirent(VFS_TO_TMPFS(mp), *dep);
411 1.2.2.2 yamt
412 1.2.2.2 yamt return 0;
413 1.2.2.2 yamt }
414 1.2.2.2 yamt
415 1.2.2.2 yamt /*
416 1.2.2.2 yamt * tmpfs_gro_lookup: Look up and save the lookup results.
417 1.2.2.2 yamt */
418 1.2.2.2 yamt static int
419 1.2.2.2 yamt tmpfs_gro_lookup(struct mount *mp, struct vnode *dvp,
420 1.2.2.2 yamt struct componentname *cnp, void *de_ret, struct vnode **vp_ret)
421 1.2.2.2 yamt {
422 1.2.2.2 yamt struct tmpfs_dirent *dirent, **dep_ret = de_ret;
423 1.2.2.2 yamt struct vnode *vp;
424 1.2.2.2 yamt int error;
425 1.2.2.2 yamt
426 1.2.2.2 yamt (void)mp;
427 1.2.2.2 yamt KASSERT(mp != NULL);
428 1.2.2.2 yamt KASSERT(dvp != NULL);
429 1.2.2.2 yamt KASSERT(cnp != NULL);
430 1.2.2.2 yamt KASSERT(dep_ret != NULL);
431 1.2.2.2 yamt KASSERT(vp_ret != NULL);
432 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
433 1.2.2.2 yamt
434 1.2.2.2 yamt dirent = tmpfs_dir_lookup(VP_TO_TMPFS_NODE(dvp), cnp);
435 1.2.2.2 yamt if (dirent == NULL)
436 1.2.2.2 yamt return ENOENT;
437 1.2.2.2 yamt
438 1.2.2.2 yamt mutex_enter(&dirent->td_node->tn_vlock);
439 1.2.2.2 yamt error = tmpfs_vnode_get(mp, dirent->td_node, &vp);
440 1.2.2.2 yamt /* Note: tmpfs_vnode_get always releases dirent->td_node->tn_vlock. */
441 1.2.2.2 yamt if (error)
442 1.2.2.2 yamt return error;
443 1.2.2.2 yamt
444 1.2.2.2 yamt KASSERT(vp != NULL);
445 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
446 1.2.2.2 yamt
447 1.2.2.2 yamt /*
448 1.2.2.2 yamt * tmpfs_vnode_get returns this locked for us, which would be
449 1.2.2.2 yamt * convenient but for lossage in other file systems. So, for
450 1.2.2.2 yamt * hysterical raisins, we have to unlock it here. The caller
451 1.2.2.2 yamt * will lock it again, and we don't rely on any interesting
452 1.2.2.2 yamt * invariants in the interim, beyond that it won't vanish from
453 1.2.2.2 yamt * under us, which it won't because it's referenced.
454 1.2.2.2 yamt *
455 1.2.2.2 yamt * XXX Once namei is fixed, we can change the genfs_rename
456 1.2.2.2 yamt * protocol so that this unlock is not necessary.
457 1.2.2.2 yamt */
458 1.2.2.2 yamt VOP_UNLOCK(vp);
459 1.2.2.2 yamt
460 1.2.2.2 yamt *dep_ret = dirent;
461 1.2.2.2 yamt *vp_ret = vp;
462 1.2.2.2 yamt return 0;
463 1.2.2.2 yamt }
464 1.2.2.2 yamt
465 1.2.2.2 yamt /*
466 1.2.2.2 yamt * tmpfs_rmdired_p: Check whether the directory vp has been rmdired.
467 1.2.2.2 yamt *
468 1.2.2.2 yamt * vp must be locked and referenced.
469 1.2.2.2 yamt */
470 1.2.2.2 yamt static bool
471 1.2.2.2 yamt tmpfs_rmdired_p(struct vnode *vp)
472 1.2.2.2 yamt {
473 1.2.2.2 yamt
474 1.2.2.2 yamt KASSERT(vp != NULL);
475 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
476 1.2.2.2 yamt KASSERT(vp->v_type == VDIR);
477 1.2.2.2 yamt
478 1.2.2.2 yamt return (VP_TO_TMPFS_NODE(vp)->tn_spec.tn_dir.tn_parent == NULL);
479 1.2.2.2 yamt }
480 1.2.2.2 yamt
481 1.2.2.2 yamt /*
482 1.2.2.2 yamt * tmpfs_gro_genealogy: Analyze the genealogy of the source and target
483 1.2.2.2 yamt * directories.
484 1.2.2.2 yamt */
485 1.2.2.2 yamt static int
486 1.2.2.2 yamt tmpfs_gro_genealogy(struct mount *mp, kauth_cred_t cred,
487 1.2.2.2 yamt struct vnode *fdvp, struct vnode *tdvp,
488 1.2.2.2 yamt struct vnode **intermediate_node_ret)
489 1.2.2.2 yamt {
490 1.2.2.2 yamt struct vnode *vp;
491 1.2.2.2 yamt struct tmpfs_node *dnode;
492 1.2.2.2 yamt int error;
493 1.2.2.2 yamt
494 1.2.2.2 yamt (void)cred;
495 1.2.2.2 yamt KASSERT(mp != NULL);
496 1.2.2.2 yamt KASSERT(fdvp != NULL);
497 1.2.2.2 yamt KASSERT(tdvp != NULL);
498 1.2.2.2 yamt KASSERT(fdvp != tdvp);
499 1.2.2.2 yamt KASSERT(intermediate_node_ret != NULL);
500 1.2.2.2 yamt KASSERT(fdvp->v_mount == mp);
501 1.2.2.2 yamt KASSERT(tdvp->v_mount == mp);
502 1.2.2.2 yamt KASSERT(fdvp->v_type == VDIR);
503 1.2.2.2 yamt KASSERT(tdvp->v_type == VDIR);
504 1.2.2.2 yamt
505 1.2.2.2 yamt /*
506 1.2.2.2 yamt * We need to provisionally lock tdvp to keep rmdir from
507 1.2.2.2 yamt * deleting it -- or any ancestor -- at an inopportune moment.
508 1.2.2.2 yamt */
509 1.2.2.2 yamt error = tmpfs_gro_lock_directory(mp, tdvp);
510 1.2.2.2 yamt if (error)
511 1.2.2.2 yamt return error;
512 1.2.2.2 yamt
513 1.2.2.2 yamt vp = tdvp;
514 1.2.2.2 yamt vref(vp);
515 1.2.2.2 yamt
516 1.2.2.2 yamt for (;;) {
517 1.2.2.2 yamt KASSERT(vp != NULL);
518 1.2.2.2 yamt KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
519 1.2.2.2 yamt KASSERT(vp->v_mount == mp);
520 1.2.2.2 yamt KASSERT(vp->v_type == VDIR);
521 1.2.2.2 yamt KASSERT(!tmpfs_rmdired_p(vp));
522 1.2.2.2 yamt
523 1.2.2.2 yamt dnode = VP_TO_TMPFS_NODE(vp)->tn_spec.tn_dir.tn_parent;
524 1.2.2.2 yamt
525 1.2.2.2 yamt /*
526 1.2.2.2 yamt * If dnode is null then vp has been rmdir'd, which is
527 1.2.2.2 yamt * not supposed to happen because we have it locked.
528 1.2.2.2 yamt */
529 1.2.2.2 yamt KASSERT(dnode != NULL);
530 1.2.2.2 yamt
531 1.2.2.2 yamt /* Did we hit the root without finding fdvp? */
532 1.2.2.2 yamt if (dnode == VP_TO_TMPFS_NODE(vp)) {
533 1.2.2.2 yamt vput(vp);
534 1.2.2.2 yamt *intermediate_node_ret = NULL;
535 1.2.2.2 yamt return 0;
536 1.2.2.2 yamt }
537 1.2.2.2 yamt
538 1.2.2.2 yamt /* Did we find that fdvp is an ancestor of tdvp? */
539 1.2.2.2 yamt if (dnode == VP_TO_TMPFS_NODE(fdvp)) {
540 1.2.2.2 yamt KASSERT(dnode->tn_vnode == fdvp);
541 1.2.2.2 yamt /* Unlock vp, but keep it referenced. */
542 1.2.2.2 yamt VOP_UNLOCK(vp);
543 1.2.2.2 yamt *intermediate_node_ret = vp;
544 1.2.2.2 yamt return 0;
545 1.2.2.2 yamt }
546 1.2.2.2 yamt
547 1.2.2.2 yamt /* Neither -- keep ascending the family tree. */
548 1.2.2.2 yamt mutex_enter(&dnode->tn_vlock);
549 1.2.2.2 yamt vput(vp);
550 1.2.2.2 yamt vp = NULL; /* Just in case, for the kassert above... */
551 1.2.2.2 yamt error = tmpfs_vnode_get(mp, dnode, &vp);
552 1.2.2.2 yamt if (error)
553 1.2.2.2 yamt return error;
554 1.2.2.2 yamt }
555 1.2.2.2 yamt }
556 1.2.2.2 yamt
557 1.2.2.2 yamt /*
558 1.2.2.2 yamt * tmpfs_gro_lock_directory: Lock the directory vp, but fail if it has
559 1.2.2.2 yamt * been rmdir'd.
560 1.2.2.2 yamt */
561 1.2.2.2 yamt static int
562 1.2.2.2 yamt tmpfs_gro_lock_directory(struct mount *mp, struct vnode *vp)
563 1.2.2.2 yamt {
564 1.2.2.2 yamt
565 1.2.2.2 yamt (void)mp;
566 1.2.2.2 yamt KASSERT(mp != NULL);
567 1.2.2.2 yamt KASSERT(vp != NULL);
568 1.2.2.2 yamt KASSERT(vp->v_mount == mp);
569 1.2.2.2 yamt
570 1.2.2.2 yamt vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
571 1.2.2.2 yamt
572 1.2.2.2 yamt if (tmpfs_rmdired_p(vp)) {
573 1.2.2.2 yamt VOP_UNLOCK(vp);
574 1.2.2.2 yamt return ENOENT;
575 1.2.2.2 yamt }
576 1.2.2.2 yamt
577 1.2.2.2 yamt return 0;
578 1.2.2.2 yamt }
579 1.2.2.2 yamt
580 1.2.2.2 yamt static const struct genfs_rename_ops tmpfs_genfs_rename_ops = {
581 1.2.2.2 yamt .gro_directory_empty_p = tmpfs_gro_directory_empty_p,
582 1.2.2.2 yamt .gro_rename_check_possible = tmpfs_gro_rename_check_possible,
583 1.2.2.2 yamt .gro_rename_check_permitted = tmpfs_gro_rename_check_permitted,
584 1.2.2.2 yamt .gro_remove_check_possible = tmpfs_gro_remove_check_possible,
585 1.2.2.2 yamt .gro_remove_check_permitted = tmpfs_gro_remove_check_permitted,
586 1.2.2.2 yamt .gro_rename = tmpfs_gro_rename,
587 1.2.2.2 yamt .gro_remove = tmpfs_gro_remove,
588 1.2.2.2 yamt .gro_lookup = tmpfs_gro_lookup,
589 1.2.2.2 yamt .gro_genealogy = tmpfs_gro_genealogy,
590 1.2.2.2 yamt .gro_lock_directory = tmpfs_gro_lock_directory,
591 1.2.2.2 yamt };
592