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