ext2fs_rename.c revision 1.2 1 1.2 riastrad /* $NetBSD: ext2fs_rename.c,v 1.2 2012/05/10 19:08:34 riastradh 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 * Ext2fs Rename
34 1.1 riastrad */
35 1.1 riastrad
36 1.1 riastrad #include <sys/cdefs.h>
37 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: ext2fs_rename.c,v 1.2 2012/05/10 19:08:34 riastradh Exp $");
38 1.1 riastrad
39 1.1 riastrad #include <sys/param.h>
40 1.1 riastrad #include <sys/buf.h>
41 1.1 riastrad #include <sys/errno.h>
42 1.1 riastrad #include <sys/kauth.h>
43 1.1 riastrad #include <sys/mount.h>
44 1.1 riastrad #include <sys/namei.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 <ufs/ext2fs/ext2fs.h>
51 1.1 riastrad #include <ufs/ext2fs/ext2fs_dir.h>
52 1.1 riastrad #include <ufs/ext2fs/ext2fs_extern.h>
53 1.1 riastrad #include <ufs/ufs/inode.h>
54 1.1 riastrad #include <ufs/ufs/ufs_extern.h>
55 1.1 riastrad #include <ufs/ufs/ufsmount.h>
56 1.1 riastrad
57 1.1 riastrad /*
58 1.1 riastrad * Forward declarations
59 1.1 riastrad */
60 1.1 riastrad static int ext2fs_sane_rename(struct vnode *, struct componentname *,
61 1.1 riastrad struct vnode *, struct componentname *,
62 1.1 riastrad kauth_cred_t, bool);
63 1.1 riastrad static bool ext2fs_rename_ulr_overlap_p(const struct ufs_lookup_results *,
64 1.1 riastrad const struct ufs_lookup_results *);
65 1.1 riastrad static int ext2fs_rename_recalculate_fulr(struct vnode *,
66 1.1 riastrad struct ufs_lookup_results *, const struct ufs_lookup_results *,
67 1.1 riastrad const struct componentname *);
68 1.1 riastrad static bool ext2fs_rmdired_p(struct vnode *);
69 1.1 riastrad static int ext2fs_read_dotdot(struct vnode *, kauth_cred_t, ino_t *);
70 1.1 riastrad static int ext2fs_rename_replace_dotdot(struct vnode *,
71 1.1 riastrad struct vnode *, struct vnode *, kauth_cred_t);
72 1.1 riastrad static int ext2fs_gro_lock_directory(struct mount *, struct vnode *);
73 1.1 riastrad
74 1.1 riastrad static const struct genfs_rename_ops ext2fs_genfs_rename_ops;
75 1.1 riastrad
76 1.1 riastrad /*
77 1.1 riastrad * ext2fs_sane_rename: The hairiest vop, with the saner API.
78 1.1 riastrad *
79 1.1 riastrad * Arguments:
80 1.1 riastrad *
81 1.1 riastrad * . fdvp (from directory vnode),
82 1.1 riastrad * . fcnp (from component name),
83 1.1 riastrad * . tdvp (to directory vnode),
84 1.1 riastrad * . tcnp (to component name),
85 1.1 riastrad * . cred (credentials structure), and
86 1.1 riastrad * . posixly_correct (flag for behaviour if target & source link same file).
87 1.1 riastrad *
88 1.1 riastrad * fdvp and tdvp may be the same, and must be referenced and unlocked.
89 1.1 riastrad */
90 1.1 riastrad static int
91 1.1 riastrad ext2fs_sane_rename(
92 1.1 riastrad struct vnode *fdvp, struct componentname *fcnp,
93 1.1 riastrad struct vnode *tdvp, struct componentname *tcnp,
94 1.1 riastrad kauth_cred_t cred, bool posixly_correct)
95 1.1 riastrad {
96 1.1 riastrad struct ufs_lookup_results fulr, tulr;
97 1.1 riastrad
98 1.1 riastrad return genfs_sane_rename(&ext2fs_genfs_rename_ops,
99 1.1 riastrad fdvp, fcnp, &fulr, tdvp, tcnp, &tulr,
100 1.1 riastrad cred, posixly_correct);
101 1.1 riastrad }
102 1.1 riastrad
103 1.1 riastrad /*
104 1.1 riastrad * ext2fs_rename: The hairiest vop, with the insanest API. Defer to
105 1.1 riastrad * genfs_insane_rename immediately.
106 1.1 riastrad */
107 1.1 riastrad int
108 1.1 riastrad ext2fs_rename(void *v)
109 1.1 riastrad {
110 1.1 riastrad
111 1.1 riastrad return genfs_insane_rename(v, &ext2fs_sane_rename);
112 1.1 riastrad }
113 1.1 riastrad
114 1.1 riastrad /*
115 1.1 riastrad * ext2fs_gro_directory_empty_p: Return true if the directory vp is
116 1.1 riastrad * empty. dvp is its parent.
117 1.1 riastrad *
118 1.1 riastrad * vp and dvp must be locked and referenced.
119 1.1 riastrad */
120 1.1 riastrad static bool
121 1.1 riastrad ext2fs_gro_directory_empty_p(struct mount *mp, kauth_cred_t cred,
122 1.1 riastrad struct vnode *vp, struct vnode *dvp)
123 1.1 riastrad {
124 1.1 riastrad
125 1.1 riastrad (void)mp;
126 1.1 riastrad KASSERT(mp != NULL);
127 1.1 riastrad KASSERT(vp != NULL);
128 1.1 riastrad KASSERT(dvp != NULL);
129 1.1 riastrad KASSERT(vp != dvp);
130 1.1 riastrad KASSERT(vp->v_mount == mp);
131 1.1 riastrad KASSERT(dvp->v_mount == mp);
132 1.1 riastrad KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
133 1.1 riastrad KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
134 1.1 riastrad
135 1.1 riastrad return ext2fs_dirempty(VTOI(vp), VTOI(dvp)->i_number, cred);
136 1.1 riastrad }
137 1.1 riastrad
138 1.1 riastrad /*
139 1.1 riastrad * ext2fs_gro_rename_check_possible: Check whether a rename is possible
140 1.1 riastrad * independent of credentials.
141 1.1 riastrad */
142 1.1 riastrad static int
143 1.1 riastrad ext2fs_gro_rename_check_possible(struct mount *mp,
144 1.1 riastrad struct vnode *fdvp, struct vnode *fvp,
145 1.1 riastrad struct vnode *tdvp, struct vnode *tvp)
146 1.1 riastrad {
147 1.1 riastrad
148 1.1 riastrad (void)mp;
149 1.1 riastrad KASSERT(mp != NULL);
150 1.1 riastrad KASSERT(fdvp != NULL);
151 1.1 riastrad KASSERT(fvp != NULL);
152 1.1 riastrad KASSERT(tdvp != NULL);
153 1.1 riastrad KASSERT(fdvp != fvp);
154 1.1 riastrad KASSERT(fdvp != tvp);
155 1.1 riastrad KASSERT(tdvp != fvp);
156 1.1 riastrad KASSERT(tdvp != tvp);
157 1.1 riastrad KASSERT(fvp != tvp);
158 1.1 riastrad KASSERT(fdvp->v_type == VDIR);
159 1.1 riastrad KASSERT(tdvp->v_type == VDIR);
160 1.1 riastrad KASSERT(fdvp->v_mount == mp);
161 1.1 riastrad KASSERT(fvp->v_mount == mp);
162 1.1 riastrad KASSERT(tdvp->v_mount == mp);
163 1.1 riastrad KASSERT((tvp == NULL) || (tvp->v_mount == mp));
164 1.1 riastrad KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
165 1.1 riastrad KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
166 1.1 riastrad KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
167 1.1 riastrad KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
168 1.1 riastrad
169 1.1 riastrad return genfs_ufslike_rename_check_possible(
170 1.1 riastrad VTOI(fdvp)->i_e2fs_flags, VTOI(fvp)->i_e2fs_flags,
171 1.1 riastrad VTOI(tdvp)->i_e2fs_flags, (tvp? VTOI(tvp)->i_e2fs_flags : 0),
172 1.1 riastrad (tvp != NULL),
173 1.1 riastrad EXT2_IMMUTABLE, EXT2_APPEND);
174 1.1 riastrad }
175 1.1 riastrad
176 1.1 riastrad /*
177 1.1 riastrad * ext2fs_gro_rename_check_permitted: Check whether a rename is
178 1.1 riastrad * permitted given our credentials.
179 1.1 riastrad */
180 1.1 riastrad static int
181 1.1 riastrad ext2fs_gro_rename_check_permitted(struct mount *mp, kauth_cred_t cred,
182 1.1 riastrad struct vnode *fdvp, struct vnode *fvp,
183 1.1 riastrad struct vnode *tdvp, struct vnode *tvp)
184 1.1 riastrad {
185 1.1 riastrad
186 1.1 riastrad (void)mp;
187 1.1 riastrad KASSERT(mp != NULL);
188 1.1 riastrad KASSERT(fdvp != NULL);
189 1.1 riastrad KASSERT(fvp != NULL);
190 1.1 riastrad KASSERT(tdvp != NULL);
191 1.1 riastrad KASSERT(fdvp != fvp);
192 1.1 riastrad KASSERT(fdvp != tvp);
193 1.1 riastrad KASSERT(tdvp != fvp);
194 1.1 riastrad KASSERT(tdvp != tvp);
195 1.1 riastrad KASSERT(fvp != tvp);
196 1.1 riastrad KASSERT(fdvp->v_type == VDIR);
197 1.1 riastrad KASSERT(tdvp->v_type == VDIR);
198 1.1 riastrad KASSERT(fdvp->v_mount == mp);
199 1.1 riastrad KASSERT(fvp->v_mount == mp);
200 1.1 riastrad KASSERT(tdvp->v_mount == mp);
201 1.1 riastrad KASSERT((tvp == NULL) || (tvp->v_mount == mp));
202 1.1 riastrad KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
203 1.1 riastrad KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
204 1.1 riastrad KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
205 1.1 riastrad KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
206 1.1 riastrad
207 1.1 riastrad return genfs_ufslike_rename_check_permitted(cred,
208 1.1 riastrad fdvp, VTOI(fdvp)->i_e2fs_mode, VTOI(fdvp)->i_uid,
209 1.1 riastrad fvp, VTOI(fvp)->i_uid,
210 1.1 riastrad tdvp, VTOI(tdvp)->i_e2fs_mode, VTOI(tdvp)->i_uid,
211 1.1 riastrad tvp, (tvp? VTOI(tvp)->i_uid : 0));
212 1.1 riastrad }
213 1.1 riastrad
214 1.1 riastrad /*
215 1.1 riastrad * ext2fs_gro_remove_check_possible: Check whether a remove is possible
216 1.1 riastrad * independent of credentials.
217 1.1 riastrad */
218 1.1 riastrad static int
219 1.1 riastrad ext2fs_gro_remove_check_possible(struct mount *mp,
220 1.1 riastrad struct vnode *dvp, struct vnode *vp)
221 1.1 riastrad {
222 1.1 riastrad
223 1.1 riastrad (void)mp;
224 1.1 riastrad KASSERT(mp != NULL);
225 1.1 riastrad KASSERT(dvp != NULL);
226 1.1 riastrad KASSERT(vp != NULL);
227 1.1 riastrad KASSERT(dvp != vp);
228 1.1 riastrad KASSERT(dvp->v_type == VDIR);
229 1.1 riastrad KASSERT(vp->v_type != VDIR);
230 1.1 riastrad KASSERT(dvp->v_mount == mp);
231 1.1 riastrad KASSERT(vp->v_mount == mp);
232 1.1 riastrad KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
233 1.1 riastrad KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
234 1.1 riastrad
235 1.1 riastrad return genfs_ufslike_remove_check_possible(
236 1.1 riastrad VTOI(dvp)->i_e2fs_flags, VTOI(vp)->i_e2fs_flags,
237 1.1 riastrad EXT2_IMMUTABLE, EXT2_APPEND);
238 1.1 riastrad }
239 1.1 riastrad
240 1.1 riastrad /*
241 1.1 riastrad * ext2fs_gro_remove_check_permitted: Check whether a remove is
242 1.1 riastrad * permitted given our credentials.
243 1.1 riastrad */
244 1.1 riastrad static int
245 1.1 riastrad ext2fs_gro_remove_check_permitted(struct mount *mp, kauth_cred_t cred,
246 1.1 riastrad struct vnode *dvp, struct vnode *vp)
247 1.1 riastrad {
248 1.1 riastrad
249 1.1 riastrad (void)mp;
250 1.1 riastrad KASSERT(mp != NULL);
251 1.1 riastrad KASSERT(dvp != NULL);
252 1.1 riastrad KASSERT(vp != NULL);
253 1.1 riastrad KASSERT(dvp != vp);
254 1.1 riastrad KASSERT(dvp->v_type == VDIR);
255 1.1 riastrad KASSERT(vp->v_type != VDIR);
256 1.1 riastrad KASSERT(dvp->v_mount == mp);
257 1.1 riastrad KASSERT(vp->v_mount == mp);
258 1.1 riastrad KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
259 1.1 riastrad KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
260 1.1 riastrad
261 1.1 riastrad return genfs_ufslike_remove_check_permitted(cred,
262 1.1 riastrad dvp, VTOI(dvp)->i_e2fs_mode, VTOI(dvp)->i_uid,
263 1.1 riastrad vp, VTOI(vp)->i_uid);
264 1.1 riastrad }
265 1.1 riastrad
266 1.1 riastrad /*
267 1.1 riastrad * ext2fs_gro_rename: Actually perform the rename operation.
268 1.1 riastrad */
269 1.1 riastrad static int
270 1.1 riastrad ext2fs_gro_rename(struct mount *mp, kauth_cred_t cred,
271 1.1 riastrad struct vnode *fdvp, struct componentname *fcnp,
272 1.1 riastrad void *fde, struct vnode *fvp,
273 1.1 riastrad struct vnode *tdvp, struct componentname *tcnp,
274 1.1 riastrad void *tde, struct vnode *tvp)
275 1.1 riastrad {
276 1.1 riastrad struct ufs_lookup_results *fulr = fde;
277 1.1 riastrad struct ufs_lookup_results *tulr = tde;
278 1.1 riastrad bool directory_p, reparent_p;
279 1.1 riastrad int error;
280 1.1 riastrad
281 1.1 riastrad (void)mp;
282 1.1 riastrad KASSERT(mp != NULL);
283 1.1 riastrad KASSERT(fdvp != NULL);
284 1.1 riastrad KASSERT(fcnp != NULL);
285 1.1 riastrad KASSERT(fulr != NULL);
286 1.1 riastrad KASSERT(fvp != NULL);
287 1.1 riastrad KASSERT(tdvp != NULL);
288 1.1 riastrad KASSERT(tcnp != NULL);
289 1.1 riastrad KASSERT(tulr != NULL);
290 1.1 riastrad KASSERT(fulr != tulr);
291 1.1 riastrad KASSERT(fdvp != fvp);
292 1.1 riastrad KASSERT(fdvp != tvp);
293 1.1 riastrad KASSERT(tdvp != fvp);
294 1.1 riastrad KASSERT(tdvp != tvp);
295 1.1 riastrad KASSERT(fvp != tvp);
296 1.1 riastrad KASSERT(fdvp->v_mount == mp);
297 1.1 riastrad KASSERT(fvp->v_mount == mp);
298 1.1 riastrad KASSERT(tdvp->v_mount == mp);
299 1.1 riastrad KASSERT((tvp == NULL) || (tvp->v_mount == mp));
300 1.1 riastrad KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
301 1.1 riastrad KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
302 1.1 riastrad KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
303 1.1 riastrad KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
304 1.1 riastrad
305 1.1 riastrad /*
306 1.1 riastrad * We shall need to temporarily bump the link count, so make
307 1.1 riastrad * sure there is room to do so.
308 1.1 riastrad */
309 1.1 riastrad if ((nlink_t)VTOI(fvp)->i_e2fs_nlink >= LINK_MAX)
310 1.1 riastrad return EMLINK;
311 1.1 riastrad
312 1.1 riastrad /*
313 1.1 riastrad * XXX There is a pile of logic here to handle a voodoo flag
314 1.1 riastrad * IN_RENAME. I think this is a vestige of days when the file
315 1.1 riastrad * system hackers didn't understand concurrency or race
316 1.1 riastrad * conditions; I believe it serves no useful function
317 1.1 riastrad * whatsoever.
318 1.1 riastrad */
319 1.1 riastrad
320 1.1 riastrad directory_p = (fvp->v_type == VDIR);
321 1.1 riastrad KASSERT(directory_p == ((VTOI(fvp)->i_e2fs_mode & IFMT) == IFDIR));
322 1.1 riastrad KASSERT((tvp == NULL) || (directory_p == (tvp->v_type == VDIR)));
323 1.1 riastrad KASSERT((tvp == NULL) || (directory_p ==
324 1.1 riastrad ((VTOI(tvp)->i_e2fs_mode & IFMT) == IFDIR)));
325 1.1 riastrad if (directory_p) {
326 1.1 riastrad if (VTOI(fvp)->i_flag & IN_RENAME)
327 1.1 riastrad return EINVAL;
328 1.1 riastrad VTOI(fvp)->i_flag |= IN_RENAME;
329 1.1 riastrad }
330 1.1 riastrad
331 1.1 riastrad reparent_p = (fdvp != tdvp);
332 1.1 riastrad KASSERT(reparent_p == (VTOI(fdvp)->i_number != VTOI(tdvp)->i_number));
333 1.1 riastrad
334 1.1 riastrad /*
335 1.1 riastrad * Commence hacking of the data on disk.
336 1.1 riastrad */
337 1.1 riastrad
338 1.1 riastrad /*
339 1.1 riastrad * 1) Bump link count while we're moving stuff
340 1.1 riastrad * around. If we crash somewhere before
341 1.1 riastrad * completing our work, the link count
342 1.1 riastrad * may be wrong, but correctable.
343 1.1 riastrad */
344 1.1 riastrad
345 1.1 riastrad KASSERT((nlink_t)VTOI(fvp)->i_e2fs_nlink < LINK_MAX);
346 1.1 riastrad VTOI(fvp)->i_e2fs_nlink++;
347 1.1 riastrad VTOI(fvp)->i_flag |= IN_CHANGE;
348 1.1 riastrad error = ext2fs_update(fvp, NULL, NULL, UPDATE_WAIT);
349 1.1 riastrad if (error)
350 1.1 riastrad goto whymustithurtsomuch;
351 1.1 riastrad
352 1.1 riastrad /*
353 1.1 riastrad * 2) If target doesn't exist, link the target
354 1.1 riastrad * to the source and unlink the source.
355 1.1 riastrad * Otherwise, rewrite the target directory
356 1.1 riastrad * entry to reference the source inode and
357 1.1 riastrad * expunge the original entry's existence.
358 1.1 riastrad */
359 1.1 riastrad
360 1.1 riastrad if (tvp == NULL) {
361 1.1 riastrad /*
362 1.1 riastrad * Account for ".." in new directory.
363 1.1 riastrad * When source and destination have the same
364 1.1 riastrad * parent we don't fool with the link count.
365 1.1 riastrad */
366 1.1 riastrad if (directory_p && reparent_p) {
367 1.1 riastrad if ((nlink_t)VTOI(tdvp)->i_e2fs_nlink >= LINK_MAX) {
368 1.1 riastrad error = EMLINK;
369 1.1 riastrad goto whymustithurtsomuch;
370 1.1 riastrad }
371 1.1 riastrad KASSERT((nlink_t)VTOI(tdvp)->i_e2fs_nlink < LINK_MAX);
372 1.1 riastrad VTOI(tdvp)->i_e2fs_nlink++;
373 1.1 riastrad VTOI(tdvp)->i_flag |= IN_CHANGE;
374 1.1 riastrad error = ext2fs_update(tdvp, NULL, NULL, UPDATE_WAIT);
375 1.1 riastrad if (error) {
376 1.1 riastrad /*
377 1.1 riastrad * Link count update didn't take --
378 1.1 riastrad * back out the in-memory link count.
379 1.1 riastrad */
380 1.1 riastrad KASSERT(0 < VTOI(tdvp)->i_e2fs_nlink);
381 1.1 riastrad VTOI(tdvp)->i_e2fs_nlink--;
382 1.1 riastrad VTOI(tdvp)->i_flag |= IN_CHANGE;
383 1.1 riastrad goto whymustithurtsomuch;
384 1.1 riastrad }
385 1.1 riastrad }
386 1.1 riastrad
387 1.1 riastrad error = ext2fs_direnter(VTOI(fvp), tdvp, tulr, tcnp);
388 1.1 riastrad if (error) {
389 1.1 riastrad if (directory_p && reparent_p) {
390 1.1 riastrad /*
391 1.1 riastrad * Directory update didn't take, but
392 1.1 riastrad * the link count update did -- back
393 1.1 riastrad * out the in-memory link count and the
394 1.1 riastrad * on-disk link count.
395 1.1 riastrad */
396 1.1 riastrad KASSERT(0 < VTOI(tdvp)->i_e2fs_nlink);
397 1.1 riastrad VTOI(tdvp)->i_e2fs_nlink--;
398 1.1 riastrad VTOI(tdvp)->i_flag |= IN_CHANGE;
399 1.1 riastrad (void)ext2fs_update(tdvp, NULL, NULL,
400 1.1 riastrad UPDATE_WAIT);
401 1.1 riastrad }
402 1.1 riastrad goto whymustithurtsomuch;
403 1.1 riastrad }
404 1.1 riastrad } else {
405 1.1 riastrad if (directory_p)
406 1.1 riastrad /* XXX WTF? Why purge here? Why not purge others? */
407 1.1 riastrad cache_purge(tdvp);
408 1.1 riastrad
409 1.1 riastrad /*
410 1.1 riastrad * Make the target directory's entry for tcnp point at
411 1.1 riastrad * the source node.
412 1.1 riastrad */
413 1.1 riastrad error = ext2fs_dirrewrite(VTOI(tdvp), tulr, VTOI(fvp), tcnp);
414 1.1 riastrad if (error)
415 1.1 riastrad goto whymustithurtsomuch;
416 1.1 riastrad
417 1.1 riastrad /*
418 1.1 riastrad * If the source and target are directories, and the
419 1.1 riastrad * target is in the same directory as the source,
420 1.1 riastrad * decrement the link count of the common parent
421 1.1 riastrad * directory, since we are removing the target from
422 1.1 riastrad * that directory.
423 1.1 riastrad */
424 1.1 riastrad if (directory_p && !reparent_p) {
425 1.1 riastrad KASSERT(fdvp == tdvp);
426 1.1 riastrad /* XXX check, don't kassert */
427 1.1 riastrad KASSERT(0 < VTOI(tdvp)->i_e2fs_nlink);
428 1.1 riastrad VTOI(tdvp)->i_e2fs_nlink--;
429 1.1 riastrad VTOI(tdvp)->i_flag |= IN_CHANGE;
430 1.1 riastrad }
431 1.1 riastrad
432 1.1 riastrad /*
433 1.1 riastrad * Adjust the link count of the target to
434 1.1 riastrad * reflect the dirrewrite above. If this is
435 1.1 riastrad * a directory it is empty and there are
436 1.1 riastrad * no links to it, so we can squash the inode and
437 1.1 riastrad * any space associated with it. We disallowed
438 1.1 riastrad * renaming over top of a directory with links to
439 1.1 riastrad * it above, as the remaining link would point to
440 1.1 riastrad * a directory without "." or ".." entries.
441 1.1 riastrad */
442 1.1 riastrad /* XXX check, don't kassert */
443 1.1 riastrad KASSERT(0 < VTOI(tvp)->i_e2fs_nlink);
444 1.1 riastrad VTOI(tvp)->i_e2fs_nlink--;
445 1.1 riastrad if (directory_p) {
446 1.1 riastrad /*
447 1.1 riastrad * XXX The ext2fs_dirempty call earlier does
448 1.1 riastrad * not guarantee anything about nlink.
449 1.1 riastrad */
450 1.1 riastrad if (VTOI(tvp)->i_e2fs_nlink != 1)
451 1.1 riastrad ufs_dirbad(VTOI(tvp), (doff_t)0,
452 1.1 riastrad "hard-linked directory");
453 1.1 riastrad VTOI(tvp)->i_e2fs_nlink = 0;
454 1.1 riastrad error = ext2fs_truncate(tvp, (off_t)0, IO_SYNC, cred);
455 1.1 riastrad #if 0 /* XXX This branch was not in ext2fs_rename! */
456 1.1 riastrad if (error)
457 1.1 riastrad goto whymustithurtsomuch;
458 1.1 riastrad #endif
459 1.1 riastrad }
460 1.1 riastrad /*
461 1.1 riastrad * XXX Why is this here, and not above the preceding
462 1.1 riastrad * conditional?
463 1.1 riastrad */
464 1.1 riastrad VTOI(tvp)->i_flag |= IN_CHANGE;
465 1.1 riastrad }
466 1.1 riastrad
467 1.1 riastrad /*
468 1.1 riastrad * If the source is a directory with a new parent, the link
469 1.1 riastrad * count of the old parent directory must be decremented and
470 1.1 riastrad * ".." set to point to the new parent.
471 1.1 riastrad */
472 1.1 riastrad if (directory_p && reparent_p) {
473 1.1 riastrad error = ext2fs_rename_replace_dotdot(fvp, fdvp, tdvp, cred);
474 1.1 riastrad if (error)
475 1.1 riastrad goto whymustithurtsomuch;
476 1.1 riastrad
477 1.1 riastrad /* XXX WTF? Why purge here? Why not purge others? */
478 1.1 riastrad cache_purge(fdvp);
479 1.1 riastrad }
480 1.1 riastrad
481 1.1 riastrad /*
482 1.1 riastrad * 3) Unlink the source.
483 1.1 riastrad */
484 1.1 riastrad
485 1.1 riastrad /*
486 1.1 riastrad * ext2fs_direnter may compact the directory in the process of
487 1.1 riastrad * inserting a new entry. That may invalidate fulr, which we
488 1.1 riastrad * need in order to remove the old entry. In that case, we
489 1.1 riastrad * need to recalculate what fulr should be.
490 1.1 riastrad *
491 1.1 riastrad * XXX I believe this is necessary only if tvp == NULL as well.
492 1.1 riastrad */
493 1.1 riastrad if (!reparent_p && ext2fs_rename_ulr_overlap_p(fulr, tulr)) {
494 1.1 riastrad error = ext2fs_rename_recalculate_fulr(fdvp, fulr, tulr, fcnp);
495 1.1 riastrad #if 0 /* XXX */
496 1.1 riastrad if (error) /* XXX Try to back out changes? */
497 1.1 riastrad goto whymustithurtsomuch;
498 1.1 riastrad #endif
499 1.1 riastrad }
500 1.1 riastrad
501 1.1 riastrad error = ext2fs_dirremove(fdvp, fulr, fcnp);
502 1.1 riastrad if (error)
503 1.1 riastrad goto whymustithurtsomuch;
504 1.1 riastrad
505 1.1 riastrad /*
506 1.1 riastrad * XXX Perhaps this should go at the top, in case the file
507 1.1 riastrad * system is modified but incompletely so because of an
508 1.1 riastrad * intermediate error.
509 1.1 riastrad */
510 1.1 riastrad genfs_rename_knote(fdvp, fvp, tdvp, tvp,
511 1.1 riastrad ((tvp != NULL) && (VTOI(tvp)->i_e2fs_nlink == 0)));
512 1.1 riastrad #if 0 /* XXX */
513 1.1 riastrad genfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
514 1.1 riastrad #endif
515 1.1 riastrad
516 1.1 riastrad whymustithurtsomuch:
517 1.1 riastrad KASSERT(0 < VTOI(fvp)->i_e2fs_nlink);
518 1.1 riastrad VTOI(fvp)->i_e2fs_nlink--;
519 1.1 riastrad VTOI(fvp)->i_flag |= IN_CHANGE;
520 1.1 riastrad if (directory_p)
521 1.1 riastrad VTOI(fvp)->i_flag &=~ IN_RENAME;
522 1.1 riastrad return error;
523 1.1 riastrad }
524 1.1 riastrad
525 1.1 riastrad /*
526 1.1 riastrad * ext2fs_rename_ulr_overlap_p: True iff tulr overlaps with fulr so
527 1.1 riastrad * that entering a directory entry at tulr may move fulr.
528 1.1 riastrad */
529 1.1 riastrad static bool
530 1.1 riastrad ext2fs_rename_ulr_overlap_p(const struct ufs_lookup_results *fulr,
531 1.1 riastrad const struct ufs_lookup_results *tulr)
532 1.1 riastrad {
533 1.1 riastrad doff_t from_prev_start, from_prev_end, to_start, to_end;
534 1.1 riastrad
535 1.1 riastrad KASSERT(fulr != NULL);
536 1.1 riastrad KASSERT(tulr != NULL);
537 1.1 riastrad KASSERT(fulr != tulr);
538 1.1 riastrad
539 1.1 riastrad /*
540 1.1 riastrad * fulr is from a DELETE lookup, so fulr->ulr_count is the size
541 1.1 riastrad * of the preceding entry (d_reclen).
542 1.1 riastrad */
543 1.1 riastrad from_prev_end = fulr->ulr_offset;
544 1.1 riastrad KASSERT(fulr->ulr_count <= from_prev_end);
545 1.1 riastrad from_prev_start = (from_prev_end - fulr->ulr_count);
546 1.1 riastrad
547 1.1 riastrad /*
548 1.1 riastrad * tulr is from a RENAME lookup, so tulr->ulr_count is the size
549 1.1 riastrad * of the free space for an entry that we are about to fill.
550 1.1 riastrad */
551 1.1 riastrad to_start = tulr->ulr_offset;
552 1.1 riastrad KASSERT(tulr->ulr_count < (EXT2FS_MAXDIRSIZE - to_start));
553 1.1 riastrad to_end = (to_start + tulr->ulr_count);
554 1.1 riastrad
555 1.1 riastrad return
556 1.1 riastrad (((to_start <= from_prev_start) && (from_prev_start < to_end)) ||
557 1.1 riastrad ((to_start <= from_prev_end) && (from_prev_end < to_end)));
558 1.1 riastrad }
559 1.1 riastrad
560 1.1 riastrad /*
561 1.1 riastrad * ext2fs_rename_recalculate_fulr: If we have just entered a directory
562 1.1 riastrad * into dvp at tulr, and we were about to remove one at fulr for an
563 1.1 riastrad * entry named fcnp, fulr may be invalid. So, if necessary,
564 1.1 riastrad * recalculate it.
565 1.1 riastrad */
566 1.1 riastrad static int
567 1.1 riastrad ext2fs_rename_recalculate_fulr(struct vnode *dvp,
568 1.1 riastrad struct ufs_lookup_results *fulr, const struct ufs_lookup_results *tulr,
569 1.1 riastrad const struct componentname *fcnp)
570 1.1 riastrad {
571 1.1 riastrad struct mount *mp;
572 1.1 riastrad struct ufsmount *ump;
573 1.1 riastrad /* XXX int is a silly type for this; blame ufsmount::um_dirblksiz. */
574 1.1 riastrad int directory_block_mask;
575 1.1 riastrad unsigned long io_block_mask;
576 1.1 riastrad doff_t offset; /* Offset of entry we're examining. */
577 1.1 riastrad doff_t search_end; /* Limit to our search. */
578 1.1 riastrad struct buf *bp; /* I/O block we're examining. */
579 1.1 riastrad char *dirbuf; /* Pointer into bp's data. */
580 1.1 riastrad doff_t dirbuf_offset; /* Offset of dirbuf from directory start. */
581 1.1 riastrad struct ext2fs_direct *ep; /* Pointer to the entry we're examining. */
582 1.1 riastrad /* XXX direct::d_reclen is 16-bit;
583 1.1 riastrad * ufs_lookup_results::ulr_reclen is 32-bit. Blah. */
584 1.1 riastrad uint32_t reclen; /* Length of the entry we're examining. */
585 1.1 riastrad uint32_t prev_reclen; /* Length of the preceding entry. */
586 1.1 riastrad int error;
587 1.1 riastrad
588 1.1 riastrad KASSERT(dvp != NULL);
589 1.1 riastrad KASSERT(dvp->v_mount != NULL);
590 1.1 riastrad KASSERT(VTOI(dvp) != NULL);
591 1.1 riastrad KASSERT(fulr != NULL);
592 1.1 riastrad KASSERT(tulr != NULL);
593 1.1 riastrad KASSERT(fulr != tulr);
594 1.1 riastrad KASSERT(ext2fs_rename_ulr_overlap_p(fulr, tulr));
595 1.1 riastrad
596 1.1 riastrad mp = dvp->v_mount;
597 1.1 riastrad ump = VFSTOUFS(mp);
598 1.1 riastrad KASSERT(ump != NULL);
599 1.1 riastrad KASSERT(ump == VTOI(dvp)->i_ump);
600 1.1 riastrad
601 1.1 riastrad KASSERT(0 < ump->um_dirblksiz);
602 1.1 riastrad KASSERT((ump->um_dirblksiz & (ump->um_dirblksiz - 1)) == 0);
603 1.1 riastrad directory_block_mask = (ump->um_dirblksiz - 1);
604 1.1 riastrad
605 1.1 riastrad KASSERT(0 < mp->mnt_stat.f_iosize);
606 1.1 riastrad KASSERT((mp->mnt_stat.f_iosize & (mp->mnt_stat.f_iosize - 1)) == 0);
607 1.1 riastrad io_block_mask = (mp->mnt_stat.f_iosize - 1);
608 1.1 riastrad
609 1.1 riastrad offset = tulr->ulr_offset;
610 1.1 riastrad KASSERT(fulr->ulr_reclen < (EXT2FS_MAXDIRSIZE - fulr->ulr_offset));
611 1.1 riastrad search_end = (fulr->ulr_offset + fulr->ulr_reclen);
612 1.1 riastrad
613 1.1 riastrad dirbuf = NULL;
614 1.1 riastrad bp = NULL;
615 1.1 riastrad dirbuf_offset = offset;
616 1.1 riastrad error = ext2fs_blkatoff(dvp, (off_t)dirbuf_offset, &dirbuf, &bp);
617 1.1 riastrad if (error)
618 1.1 riastrad return error;
619 1.1 riastrad KASSERT(dirbuf != NULL);
620 1.1 riastrad KASSERT(bp != NULL);
621 1.1 riastrad
622 1.1 riastrad prev_reclen = fulr->ulr_count;
623 1.1 riastrad
624 1.1 riastrad /*
625 1.1 riastrad * Search from offset to search_end for the entry matching
626 1.1 riastrad * fcnp, which must be there because we found it before and it
627 1.1 riastrad * should only at most have moved earlier.
628 1.1 riastrad */
629 1.1 riastrad for (;;) {
630 1.1 riastrad KASSERT(offset < search_end);
631 1.1 riastrad
632 1.1 riastrad /*
633 1.1 riastrad * If we are at an I/O block boundary, fetch the next block.
634 1.1 riastrad */
635 1.1 riastrad if ((offset & io_block_mask) == 0) {
636 1.1 riastrad #ifdef DIAGNOSTIC /* XXX */
637 1.1 riastrad printf("%s: directory block of inode 0x%llx"
638 1.1 riastrad " extends across I/O block boundary,"
639 1.1 riastrad " which shouldn't happen!\n",
640 1.1 riastrad mp->mnt_stat.f_mntonname,
641 1.1 riastrad (unsigned long long)VTOI(dvp)->i_number);
642 1.1 riastrad #endif
643 1.1 riastrad brelse(bp, 0);
644 1.1 riastrad dirbuf = NULL;
645 1.1 riastrad bp = NULL;
646 1.1 riastrad dirbuf_offset = offset;
647 1.1 riastrad error = ext2fs_blkatoff(dvp, (off_t)dirbuf_offset,
648 1.1 riastrad &dirbuf, &bp);
649 1.1 riastrad if (error)
650 1.1 riastrad return error;
651 1.1 riastrad KASSERT(dirbuf != NULL);
652 1.1 riastrad KASSERT(bp != NULL);
653 1.1 riastrad }
654 1.1 riastrad
655 1.1 riastrad /*
656 1.1 riastrad * Examine the directory entry at offset.
657 1.1 riastrad */
658 1.1 riastrad KASSERT(dirbuf_offset <= offset);
659 1.1 riastrad ep = (struct ext2fs_direct *)
660 1.1 riastrad (dirbuf + (offset - dirbuf_offset));
661 1.2 riastrad reclen = fs2h16(ep->e2d_reclen);
662 1.1 riastrad
663 1.1 riastrad if (ep->e2d_ino == 0)
664 1.1 riastrad goto next; /* Entry is unused. */
665 1.1 riastrad
666 1.2 riastrad if (fs2h32(ep->e2d_ino) == WINO)
667 1.1 riastrad goto next; /* Entry is whiteout. */
668 1.1 riastrad
669 1.1 riastrad if (fcnp->cn_namelen != ep->e2d_namlen)
670 1.1 riastrad goto next; /* Wrong name length. */
671 1.1 riastrad
672 1.1 riastrad if (memcmp(ep->e2d_name, fcnp->cn_nameptr, fcnp->cn_namelen))
673 1.1 riastrad goto next; /* Wrong name. */
674 1.1 riastrad
675 1.1 riastrad /* Got it! */
676 1.1 riastrad break;
677 1.1 riastrad
678 1.1 riastrad next:
679 1.1 riastrad if (! ((reclen < search_end) &&
680 1.1 riastrad (offset < (search_end - reclen)))) {
681 1.1 riastrad brelse(bp, 0);
682 1.1 riastrad return EIO; /* XXX Panic? What? */
683 1.1 riastrad }
684 1.1 riastrad
685 1.1 riastrad KASSERT(reclen < search_end);
686 1.1 riastrad KASSERT(offset < (search_end - reclen));
687 1.1 riastrad prev_reclen = reclen;
688 1.1 riastrad offset += reclen;
689 1.1 riastrad }
690 1.1 riastrad
691 1.1 riastrad /*
692 1.1 riastrad * Found the entry. Record where.
693 1.1 riastrad */
694 1.1 riastrad fulr->ulr_offset = offset;
695 1.1 riastrad fulr->ulr_reclen = reclen;
696 1.1 riastrad
697 1.1 riastrad /*
698 1.1 riastrad * Record the preceding record length, but not if we're at the
699 1.1 riastrad * start of a directory block.
700 1.1 riastrad */
701 1.1 riastrad fulr->ulr_count = ((offset & directory_block_mask)? prev_reclen : 0);
702 1.1 riastrad
703 1.1 riastrad brelse(bp, 0);
704 1.1 riastrad return 0;
705 1.1 riastrad }
706 1.1 riastrad
707 1.1 riastrad /*
708 1.1 riastrad * ext2fs_gro_remove: Rename an object over another link to itself,
709 1.1 riastrad * effectively removing just the original link.
710 1.1 riastrad */
711 1.1 riastrad static int
712 1.1 riastrad ext2fs_gro_remove(struct mount *mp, kauth_cred_t cred,
713 1.1 riastrad struct vnode *dvp, struct componentname *cnp, void *de, struct vnode *vp)
714 1.1 riastrad {
715 1.1 riastrad struct ufs_lookup_results *ulr = de;
716 1.1 riastrad int error;
717 1.1 riastrad
718 1.1 riastrad (void)mp;
719 1.1 riastrad KASSERT(mp != NULL);
720 1.1 riastrad KASSERT(dvp != NULL);
721 1.1 riastrad KASSERT(cnp != NULL);
722 1.1 riastrad KASSERT(ulr != NULL);
723 1.1 riastrad KASSERT(vp != NULL);
724 1.1 riastrad KASSERT(dvp != vp);
725 1.1 riastrad KASSERT(dvp->v_mount == mp);
726 1.1 riastrad KASSERT(vp->v_mount == mp);
727 1.1 riastrad KASSERT(dvp->v_type == VDIR);
728 1.1 riastrad KASSERT(vp->v_type != VDIR);
729 1.1 riastrad KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
730 1.1 riastrad KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
731 1.1 riastrad
732 1.1 riastrad error = ext2fs_dirremove(dvp, ulr, cnp);
733 1.1 riastrad if (error)
734 1.1 riastrad return error;
735 1.1 riastrad
736 1.1 riastrad KASSERT(0 < VTOI(vp)->i_e2fs_nlink);
737 1.1 riastrad VTOI(vp)->i_e2fs_nlink--;
738 1.1 riastrad VTOI(vp)->i_flag |= IN_CHANGE;
739 1.1 riastrad
740 1.1 riastrad VN_KNOTE(dvp, NOTE_WRITE);
741 1.1 riastrad VN_KNOTE(vp, (VTOI(vp)->i_e2fs_nlink? NOTE_LINK : NOTE_DELETE));
742 1.1 riastrad
743 1.1 riastrad return 0;
744 1.1 riastrad }
745 1.1 riastrad
746 1.1 riastrad /*
747 1.1 riastrad * ext2fs_gro_lookup: Look up and save the lookup results.
748 1.1 riastrad */
749 1.1 riastrad static int
750 1.1 riastrad ext2fs_gro_lookup(struct mount *mp, struct vnode *dvp,
751 1.1 riastrad struct componentname *cnp, void *de_ret, struct vnode **vp_ret)
752 1.1 riastrad {
753 1.1 riastrad struct ufs_lookup_results *ulr_ret = de_ret;
754 1.1 riastrad struct vnode *vp;
755 1.1 riastrad int error;
756 1.1 riastrad
757 1.1 riastrad (void)mp;
758 1.1 riastrad KASSERT(mp != NULL);
759 1.1 riastrad KASSERT(dvp != NULL);
760 1.1 riastrad KASSERT(cnp != NULL);
761 1.1 riastrad KASSERT(ulr_ret != NULL);
762 1.1 riastrad KASSERT(vp_ret != NULL);
763 1.1 riastrad KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
764 1.1 riastrad
765 1.1 riastrad /* Kludge cargo-culted from dholland's ufs_rename. */
766 1.1 riastrad cnp->cn_flags &=~ MODMASK;
767 1.1 riastrad cnp->cn_flags |= (LOCKPARENT | LOCKLEAF);
768 1.1 riastrad
769 1.1 riastrad error = relookup(dvp, &vp, cnp, 0 /* dummy */);
770 1.1 riastrad if ((error == 0) && (vp == NULL)) {
771 1.1 riastrad error = ENOENT;
772 1.1 riastrad goto out;
773 1.1 riastrad } else if (error) {
774 1.1 riastrad return error;
775 1.1 riastrad }
776 1.1 riastrad
777 1.1 riastrad /*
778 1.1 riastrad * Thanks to VFS insanity, relookup locks vp, which screws us
779 1.1 riastrad * in various ways.
780 1.1 riastrad */
781 1.1 riastrad KASSERT(vp != NULL);
782 1.1 riastrad VOP_UNLOCK(vp);
783 1.1 riastrad
784 1.1 riastrad out: *ulr_ret = VTOI(dvp)->i_crap;
785 1.1 riastrad *vp_ret = vp;
786 1.1 riastrad return error;
787 1.1 riastrad }
788 1.1 riastrad
789 1.1 riastrad /*
790 1.1 riastrad * ext2fs_rmdired_p: Check whether the directory vp has been rmdired.
791 1.1 riastrad *
792 1.1 riastrad * vp must be locked and referenced.
793 1.1 riastrad */
794 1.1 riastrad static bool
795 1.1 riastrad ext2fs_rmdired_p(struct vnode *vp)
796 1.1 riastrad {
797 1.1 riastrad
798 1.1 riastrad KASSERT(vp != NULL);
799 1.1 riastrad KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
800 1.1 riastrad KASSERT(vp->v_type == VDIR);
801 1.1 riastrad
802 1.1 riastrad /* XXX Is this correct? */
803 1.1 riastrad return (ext2fs_size(VTOI(vp)) == 0);
804 1.1 riastrad }
805 1.1 riastrad
806 1.1 riastrad /*
807 1.1 riastrad * ext2fs_gro_genealogy: Analyze the genealogy of the source and target
808 1.1 riastrad * directories.
809 1.1 riastrad */
810 1.1 riastrad static int
811 1.1 riastrad ext2fs_gro_genealogy(struct mount *mp, kauth_cred_t cred,
812 1.1 riastrad struct vnode *fdvp, struct vnode *tdvp,
813 1.1 riastrad struct vnode **intermediate_node_ret)
814 1.1 riastrad {
815 1.1 riastrad struct vnode *vp, *dvp;
816 1.1 riastrad ino_t dotdot_ino;
817 1.1 riastrad int error;
818 1.1 riastrad
819 1.1 riastrad KASSERT(mp != NULL);
820 1.1 riastrad KASSERT(fdvp != NULL);
821 1.1 riastrad KASSERT(tdvp != NULL);
822 1.1 riastrad KASSERT(fdvp != tdvp);
823 1.1 riastrad KASSERT(intermediate_node_ret != NULL);
824 1.1 riastrad KASSERT(fdvp->v_mount == mp);
825 1.1 riastrad KASSERT(tdvp->v_mount == mp);
826 1.1 riastrad KASSERT(fdvp->v_type == VDIR);
827 1.1 riastrad KASSERT(tdvp->v_type == VDIR);
828 1.1 riastrad
829 1.1 riastrad /*
830 1.1 riastrad * We need to provisionally lock tdvp to keep rmdir from
831 1.1 riastrad * deleting it -- or any ancestor -- at an inopportune moment.
832 1.1 riastrad */
833 1.1 riastrad error = ext2fs_gro_lock_directory(mp, tdvp);
834 1.1 riastrad if (error)
835 1.1 riastrad return error;
836 1.1 riastrad
837 1.1 riastrad vp = tdvp;
838 1.1 riastrad vref(vp);
839 1.1 riastrad
840 1.1 riastrad for (;;) {
841 1.1 riastrad KASSERT(vp != NULL);
842 1.1 riastrad KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
843 1.1 riastrad KASSERT(vp->v_mount == mp);
844 1.1 riastrad KASSERT(vp->v_type == VDIR);
845 1.1 riastrad KASSERT(!ext2fs_rmdired_p(vp));
846 1.1 riastrad
847 1.1 riastrad /* Did we hit the root without finding fdvp? */
848 1.1 riastrad if (VTOI(vp)->i_number == ROOTINO) {
849 1.1 riastrad vput(vp);
850 1.1 riastrad *intermediate_node_ret = NULL;
851 1.1 riastrad return 0;
852 1.1 riastrad }
853 1.1 riastrad
854 1.1 riastrad error = ext2fs_read_dotdot(vp, cred, &dotdot_ino);
855 1.1 riastrad if (error) {
856 1.1 riastrad vput(vp);
857 1.1 riastrad return error;
858 1.1 riastrad }
859 1.1 riastrad
860 1.1 riastrad /* Did we find that fdvp is an ancestor of tdvp? */
861 1.1 riastrad if (VTOI(fdvp)->i_number == dotdot_ino) {
862 1.1 riastrad /* Unlock vp, but keep it referenced. */
863 1.1 riastrad VOP_UNLOCK(vp);
864 1.1 riastrad *intermediate_node_ret = vp;
865 1.1 riastrad return 0;
866 1.1 riastrad }
867 1.1 riastrad
868 1.1 riastrad /* Neither -- keep ascending the family tree. */
869 1.1 riastrad
870 1.1 riastrad /*
871 1.1 riastrad * Unlock vp so that we can lock the parent, but keep
872 1.1 riastrad * vp referenced until after we have found the parent,
873 1.1 riastrad * so that dotdot_ino will not be recycled.
874 1.1 riastrad *
875 1.1 riastrad * XXX This guarantees that vp's inode number will not
876 1.1 riastrad * be recycled, but why can't dotdot_ino be recycled?
877 1.1 riastrad */
878 1.1 riastrad VOP_UNLOCK(vp);
879 1.1 riastrad error = VFS_VGET(mp, dotdot_ino, &dvp);
880 1.1 riastrad vrele(vp);
881 1.1 riastrad if (error)
882 1.1 riastrad return error;
883 1.1 riastrad
884 1.1 riastrad KASSERT(dvp != NULL);
885 1.1 riastrad KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
886 1.1 riastrad vp = dvp;
887 1.1 riastrad
888 1.1 riastrad if (vp->v_type != VDIR) {
889 1.1 riastrad /*
890 1.1 riastrad * XXX Panic? Print a warning? Can this
891 1.1 riastrad * happen if we lose the race I suspect to
892 1.1 riastrad * exist above, and the `..' inode number has
893 1.1 riastrad * been recycled?
894 1.1 riastrad */
895 1.1 riastrad vput(vp);
896 1.1 riastrad return ENOTDIR;
897 1.1 riastrad }
898 1.1 riastrad
899 1.1 riastrad if (ext2fs_rmdired_p(vp)) {
900 1.1 riastrad vput(vp);
901 1.1 riastrad return ENOENT;
902 1.1 riastrad }
903 1.1 riastrad }
904 1.1 riastrad }
905 1.1 riastrad
906 1.1 riastrad /*
907 1.1 riastrad * ext2fs_read_dotdot: Store in *ino_ret the inode number of the parent
908 1.1 riastrad * of the directory vp.
909 1.1 riastrad */
910 1.1 riastrad static int
911 1.1 riastrad ext2fs_read_dotdot(struct vnode *vp, kauth_cred_t cred, ino_t *ino_ret)
912 1.1 riastrad {
913 1.1 riastrad struct ext2fs_dirtemplate dirbuf;
914 1.1 riastrad int error;
915 1.1 riastrad
916 1.1 riastrad KASSERT(vp != NULL);
917 1.1 riastrad KASSERT(ino_ret != NULL);
918 1.1 riastrad KASSERT(vp->v_type == VDIR);
919 1.1 riastrad
920 1.1 riastrad error = vn_rdwr(UIO_READ, vp, &dirbuf, sizeof dirbuf, (off_t)0,
921 1.1 riastrad UIO_SYSSPACE, IO_NODELOCKED, cred, NULL, NULL);
922 1.1 riastrad if (error)
923 1.1 riastrad return error;
924 1.1 riastrad
925 1.1 riastrad if (dirbuf.dotdot_namlen != 2 ||
926 1.1 riastrad dirbuf.dotdot_name[0] != '.' ||
927 1.1 riastrad dirbuf.dotdot_name[1] != '.')
928 1.1 riastrad /* XXX Panic? Print warning? */
929 1.1 riastrad return ENOTDIR;
930 1.1 riastrad
931 1.1 riastrad *ino_ret = fs2h32(dirbuf.dotdot_ino);
932 1.1 riastrad return 0;
933 1.1 riastrad }
934 1.1 riastrad
935 1.1 riastrad /*
936 1.1 riastrad * ext2fs_rename_replace_dotdot: Change the target of the `..' entry of
937 1.1 riastrad * the directory vp from fdvp to tdvp.
938 1.1 riastrad */
939 1.1 riastrad static int
940 1.1 riastrad ext2fs_rename_replace_dotdot(struct vnode *vp,
941 1.1 riastrad struct vnode *fdvp, struct vnode *tdvp,
942 1.1 riastrad kauth_cred_t cred)
943 1.1 riastrad {
944 1.1 riastrad struct ext2fs_dirtemplate dirbuf;
945 1.1 riastrad int error;
946 1.1 riastrad
947 1.1 riastrad /* XXX Does it make sense to do this before the sanity checks below? */
948 1.1 riastrad KASSERT(0 < VTOI(fdvp)->i_e2fs_nlink);
949 1.1 riastrad VTOI(fdvp)->i_e2fs_nlink--;
950 1.1 riastrad VTOI(fdvp)->i_flag |= IN_CHANGE;
951 1.1 riastrad
952 1.1 riastrad error = vn_rdwr(UIO_READ, vp, &dirbuf, sizeof dirbuf, (off_t)0,
953 1.1 riastrad UIO_SYSSPACE, IO_NODELOCKED, cred, NULL, NULL);
954 1.1 riastrad if (error)
955 1.1 riastrad return error;
956 1.1 riastrad
957 1.1 riastrad if (dirbuf.dotdot_namlen != 2 ||
958 1.1 riastrad dirbuf.dotdot_name[0] != '.' ||
959 1.1 riastrad dirbuf.dotdot_name[1] != '.') {
960 1.1 riastrad ufs_dirbad(VTOI(vp), (doff_t)12, "bad `..' entry");
961 1.1 riastrad return 0;
962 1.1 riastrad }
963 1.1 riastrad
964 1.1 riastrad if (fs2h32(dirbuf.dotdot_ino) != VTOI(fdvp)->i_number) {
965 1.1 riastrad ufs_dirbad(VTOI(vp), (doff_t)12,
966 1.1 riastrad "`..' does not point at parent");
967 1.1 riastrad return 0;
968 1.1 riastrad }
969 1.1 riastrad
970 1.1 riastrad dirbuf.dotdot_ino = h2fs32(VTOI(tdvp)->i_number);
971 1.1 riastrad /* XXX WTF? Why not check error? */
972 1.1 riastrad (void)vn_rdwr(UIO_WRITE, vp, &dirbuf, sizeof dirbuf, (off_t)0,
973 1.1 riastrad UIO_SYSSPACE, (IO_NODELOCKED | IO_SYNC), cred, NULL, NULL);
974 1.1 riastrad
975 1.1 riastrad return 0;
976 1.1 riastrad }
977 1.1 riastrad
978 1.1 riastrad /*
979 1.1 riastrad * ext2fs_gro_lock_directory: Lock the directory vp, but fail if it has
980 1.1 riastrad * been rmdir'd.
981 1.1 riastrad */
982 1.1 riastrad static int
983 1.1 riastrad ext2fs_gro_lock_directory(struct mount *mp, struct vnode *vp)
984 1.1 riastrad {
985 1.1 riastrad
986 1.1 riastrad (void)mp;
987 1.1 riastrad KASSERT(mp != NULL);
988 1.1 riastrad KASSERT(vp != NULL);
989 1.1 riastrad KASSERT(vp->v_mount == mp);
990 1.1 riastrad
991 1.1 riastrad vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
992 1.1 riastrad
993 1.1 riastrad if (ext2fs_rmdired_p(vp)) {
994 1.1 riastrad VOP_UNLOCK(vp);
995 1.1 riastrad return ENOENT;
996 1.1 riastrad }
997 1.1 riastrad
998 1.1 riastrad return 0;
999 1.1 riastrad }
1000 1.1 riastrad
1001 1.1 riastrad static const struct genfs_rename_ops ext2fs_genfs_rename_ops = {
1002 1.1 riastrad .gro_directory_empty_p = ext2fs_gro_directory_empty_p,
1003 1.1 riastrad .gro_rename_check_possible = ext2fs_gro_rename_check_possible,
1004 1.1 riastrad .gro_rename_check_permitted = ext2fs_gro_rename_check_permitted,
1005 1.1 riastrad .gro_remove_check_possible = ext2fs_gro_remove_check_possible,
1006 1.1 riastrad .gro_remove_check_permitted = ext2fs_gro_remove_check_permitted,
1007 1.1 riastrad .gro_rename = ext2fs_gro_rename,
1008 1.1 riastrad .gro_remove = ext2fs_gro_remove,
1009 1.1 riastrad .gro_lookup = ext2fs_gro_lookup,
1010 1.1 riastrad .gro_genealogy = ext2fs_gro_genealogy,
1011 1.1 riastrad .gro_lock_directory = ext2fs_gro_lock_directory,
1012 1.1 riastrad };
1013