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