udf_rename.c revision 1.1 1 1.1 reinoud /* $NetBSD: udf_rename.c,v 1.1 2013/07/10 15:10:56 reinoud Exp $ */
2 1.1 reinoud
3 1.1 reinoud /*
4 1.1 reinoud * Copyright (c) 2013 Reinoud Zandijk
5 1.1 reinoud * All rights reserved.
6 1.1 reinoud *
7 1.1 reinoud * Redistribution and use in source and binary forms, with or without
8 1.1 reinoud * modification, are permitted provided that the following conditions
9 1.1 reinoud * are met:
10 1.1 reinoud * 1. Redistributions of source code must retain the above copyright
11 1.1 reinoud * notice, this list of conditions and the following disclaimer.
12 1.1 reinoud * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 reinoud * notice, this list of conditions and the following disclaimer in the
14 1.1 reinoud * documentation and/or other materials provided with the distribution.
15 1.1 reinoud *
16 1.1 reinoud * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 reinoud * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 reinoud * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 reinoud * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 reinoud * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 reinoud * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 reinoud * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 reinoud * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 reinoud * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 reinoud * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 reinoud *
27 1.1 reinoud * Comments and trivial code from the reference implementation in tmpfs.
28 1.1 reinoud */
29 1.1 reinoud
30 1.1 reinoud #include <sys/cdefs.h>
31 1.1 reinoud __KERNEL_RCSID(0, "$NetBSD: udf_rename.c,v 1.1 2013/07/10 15:10:56 reinoud Exp $");
32 1.1 reinoud
33 1.1 reinoud #include <sys/param.h>
34 1.1 reinoud #include <sys/errno.h>
35 1.1 reinoud #include <sys/kauth.h>
36 1.1 reinoud #include <sys/mount.h>
37 1.1 reinoud #include <sys/namei.h>
38 1.1 reinoud #include <sys/stat.h>
39 1.1 reinoud #include <sys/malloc.h>
40 1.1 reinoud #include <sys/dirent.h>
41 1.1 reinoud #include <sys/vnode.h>
42 1.1 reinoud #include <sys/vnode_if.h>
43 1.1 reinoud
44 1.1 reinoud #include <miscfs/genfs/genfs.h>
45 1.1 reinoud
46 1.1 reinoud #include <fs/udf/ecma167-udf.h>
47 1.1 reinoud #include <fs/udf/udf_mount.h>
48 1.1 reinoud #include <sys/dirhash.h>
49 1.1 reinoud
50 1.1 reinoud #include "udf.h"
51 1.1 reinoud #include "udf_subr.h"
52 1.1 reinoud #include "udf_bswap.h"
53 1.1 reinoud
54 1.1 reinoud
55 1.1 reinoud /* forwards */
56 1.1 reinoud static int udf_sane_rename( struct vnode *, struct componentname *,
57 1.1 reinoud struct vnode *, struct componentname *,
58 1.1 reinoud kauth_cred_t, bool);
59 1.1 reinoud static bool udf_rmdired_p(struct vnode *);
60 1.1 reinoud static int udf_gro_lock_directory(struct mount *, struct vnode *);
61 1.1 reinoud
62 1.1 reinoud static const struct genfs_rename_ops udf_genfs_rename_ops;
63 1.1 reinoud
64 1.1 reinoud
65 1.1 reinoud #define VTOI(vnode) ((struct udf_node *) (vnode)->v_data)
66 1.1 reinoud
67 1.1 reinoud
68 1.1 reinoud /*
69 1.1 reinoud * udf_sane_rename: The hairiest vop, with the saner API.
70 1.1 reinoud *
71 1.1 reinoud * Arguments:
72 1.1 reinoud *
73 1.1 reinoud * . fdvp (from directory vnode),
74 1.1 reinoud * . fcnp (from component name),
75 1.1 reinoud * . tdvp (to directory vnode),
76 1.1 reinoud * . tcnp (to component name),
77 1.1 reinoud * . cred (credentials structure), and
78 1.1 reinoud * . posixly_correct (flag for behaviour if target & source link same file).
79 1.1 reinoud *
80 1.1 reinoud * fdvp and tdvp may be the same, and must be referenced and unlocked.
81 1.1 reinoud */
82 1.1 reinoud static int
83 1.1 reinoud udf_sane_rename( struct vnode *fdvp, struct componentname *fcnp,
84 1.1 reinoud struct vnode *tdvp, struct componentname *tcnp,
85 1.1 reinoud kauth_cred_t cred, bool posixly_correct)
86 1.1 reinoud {
87 1.1 reinoud struct dirent fdirent, tdirent; /* XXX OK? XXX */
88 1.1 reinoud
89 1.1 reinoud DPRINTF(CALL, ("udf_sane_rename '%s' -> '%s'\n",
90 1.1 reinoud fcnp->cn_nameptr, tcnp->cn_nameptr));
91 1.1 reinoud return genfs_sane_rename(&udf_genfs_rename_ops,
92 1.1 reinoud fdvp, fcnp, &fdirent, tdvp, tcnp, &tdirent,
93 1.1 reinoud cred, posixly_correct);
94 1.1 reinoud }
95 1.1 reinoud
96 1.1 reinoud
97 1.1 reinoud /*
98 1.1 reinoud * udf_rename: the hairiest vop, with the insanest API. Pass to
99 1.1 reinoud * genfs_insane_rename immediately.
100 1.1 reinoud */
101 1.1 reinoud int
102 1.1 reinoud udf_rename(void *v)
103 1.1 reinoud {
104 1.1 reinoud struct vop_rename_args /* {
105 1.1 reinoud struct vnode *a_fdvp;
106 1.1 reinoud struct vnode *a_fvp;
107 1.1 reinoud struct componentname *a_fcnp;
108 1.1 reinoud struct vnode *a_tdvp;
109 1.1 reinoud struct vnode *a_tvp;
110 1.1 reinoud struct componentname *a_tcnp;
111 1.1 reinoud } */ *ap = v;
112 1.1 reinoud DPRINTF(CALL, ("udf_rename called\n"));
113 1.1 reinoud return genfs_insane_rename(ap, &udf_sane_rename);
114 1.1 reinoud }
115 1.1 reinoud
116 1.1 reinoud
117 1.1 reinoud /*
118 1.1 reinoud * udf_gro_directory_empty_p: return true if the directory vp is empty. dvp is
119 1.1 reinoud * its parent.
120 1.1 reinoud *
121 1.1 reinoud * vp and dvp must be locked and referenced.
122 1.1 reinoud */
123 1.1 reinoud static bool
124 1.1 reinoud udf_gro_directory_empty_p(struct mount *mp, kauth_cred_t cred,
125 1.1 reinoud struct vnode *vp, struct vnode *dvp)
126 1.1 reinoud {
127 1.1 reinoud struct udf_node *udf_node = VTOI(vp);
128 1.1 reinoud int error, isempty;
129 1.1 reinoud
130 1.1 reinoud KASSERT(mp != NULL);
131 1.1 reinoud KASSERT(vp != NULL);
132 1.1 reinoud KASSERT(dvp != NULL);
133 1.1 reinoud KASSERT(vp != dvp);
134 1.1 reinoud KASSERT(vp->v_mount == mp);
135 1.1 reinoud KASSERT(dvp->v_mount == mp);
136 1.1 reinoud KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
137 1.1 reinoud KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
138 1.1 reinoud
139 1.1 reinoud DPRINTF(CALL, ("udf_gro_directory_empty_p called\n"));
140 1.1 reinoud /* make sure our `leaf' node's hash is populated */
141 1.1 reinoud dirhash_get(&udf_node->dir_hash);
142 1.1 reinoud error = udf_dirhash_fill(udf_node);
143 1.1 reinoud if (error) {
144 1.1 reinoud dirhash_put(udf_node->dir_hash);
145 1.1 reinoud return error;
146 1.1 reinoud }
147 1.1 reinoud
148 1.1 reinoud /* check to see if the directory is empty */
149 1.1 reinoud isempty = dirhash_dir_isempty(udf_node->dir_hash);
150 1.1 reinoud dirhash_put(udf_node->dir_hash);
151 1.1 reinoud
152 1.1 reinoud return isempty;
153 1.1 reinoud }
154 1.1 reinoud
155 1.1 reinoud
156 1.1 reinoud /*
157 1.1 reinoud * udf_gro_rename_check_possible: check whether a rename is possible
158 1.1 reinoud * independent of credentials.
159 1.1 reinoud */
160 1.1 reinoud static int
161 1.1 reinoud udf_gro_rename_check_possible(struct mount *mp,
162 1.1 reinoud struct vnode *fdvp, struct vnode *fvp,
163 1.1 reinoud struct vnode *tdvp, struct vnode *tvp)
164 1.1 reinoud {
165 1.1 reinoud (void)mp;
166 1.1 reinoud KASSERT(mp != NULL);
167 1.1 reinoud KASSERT(fdvp != NULL);
168 1.1 reinoud KASSERT(fvp != NULL);
169 1.1 reinoud KASSERT(tdvp != NULL);
170 1.1 reinoud KASSERT(fdvp != fvp);
171 1.1 reinoud KASSERT(fdvp != tvp);
172 1.1 reinoud KASSERT(tdvp != fvp);
173 1.1 reinoud KASSERT(tdvp != tvp);
174 1.1 reinoud KASSERT(fvp != tvp);
175 1.1 reinoud KASSERT(fdvp->v_type == VDIR);
176 1.1 reinoud KASSERT(tdvp->v_type == VDIR);
177 1.1 reinoud KASSERT(fdvp->v_mount == mp);
178 1.1 reinoud KASSERT(fvp->v_mount == mp);
179 1.1 reinoud KASSERT(tdvp->v_mount == mp);
180 1.1 reinoud KASSERT((tvp == NULL) || (tvp->v_mount == mp));
181 1.1 reinoud KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
182 1.1 reinoud KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
183 1.1 reinoud KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
184 1.1 reinoud KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
185 1.1 reinoud
186 1.1 reinoud DPRINTF(CALL, ("udf_gro_rename_check_possible called\n"));
187 1.1 reinoud if (mp->mnt_flag & MNT_RDONLY)
188 1.1 reinoud return EROFS;
189 1.1 reinoud
190 1.1 reinoud /* flags not implemented since they are not defined (yet) in UDF */
191 1.1 reinoud return 0;
192 1.1 reinoud }
193 1.1 reinoud
194 1.1 reinoud
195 1.1 reinoud /*
196 1.1 reinoud * udf_gro_rename_check_permitted: check whether a rename is permitted given
197 1.1 reinoud * our credentials.
198 1.1 reinoud */
199 1.1 reinoud static int
200 1.1 reinoud udf_gro_rename_check_permitted(struct mount *mp, kauth_cred_t cred,
201 1.1 reinoud struct vnode *fdvp, struct vnode *fvp,
202 1.1 reinoud struct vnode *tdvp, struct vnode *tvp)
203 1.1 reinoud {
204 1.1 reinoud struct udf_node *fdir_node = VTOI(fdvp);
205 1.1 reinoud struct udf_node *tdir_node = VTOI(tdvp);
206 1.1 reinoud struct udf_node *f_node = VTOI(fvp);
207 1.1 reinoud struct udf_node *t_node = (tvp? VTOI(tvp): NULL);
208 1.1 reinoud mode_t fdmode, tdmode;
209 1.1 reinoud uid_t fduid, tduid, fuid, tuid;
210 1.1 reinoud gid_t gdummy;
211 1.1 reinoud
212 1.1 reinoud (void)mp;
213 1.1 reinoud KASSERT(mp != NULL);
214 1.1 reinoud KASSERT(fdvp != NULL);
215 1.1 reinoud KASSERT(fvp != NULL);
216 1.1 reinoud KASSERT(tdvp != NULL);
217 1.1 reinoud KASSERT(fdvp != fvp);
218 1.1 reinoud KASSERT(fdvp != tvp);
219 1.1 reinoud KASSERT(tdvp != fvp);
220 1.1 reinoud KASSERT(tdvp != tvp);
221 1.1 reinoud KASSERT(fvp != tvp);
222 1.1 reinoud KASSERT(fdvp->v_type == VDIR);
223 1.1 reinoud KASSERT(tdvp->v_type == VDIR);
224 1.1 reinoud KASSERT(fdvp->v_mount == mp);
225 1.1 reinoud KASSERT(fvp->v_mount == mp);
226 1.1 reinoud KASSERT(tdvp->v_mount == mp);
227 1.1 reinoud KASSERT((tvp == NULL) || (tvp->v_mount == mp));
228 1.1 reinoud KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
229 1.1 reinoud KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
230 1.1 reinoud KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
231 1.1 reinoud KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
232 1.1 reinoud
233 1.1 reinoud DPRINTF(CALL, ("udf_gro_rename_check_permitted called\n"));
234 1.1 reinoud fdmode = udf_getaccessmode(fdir_node);
235 1.1 reinoud tdmode = udf_getaccessmode(tdir_node);
236 1.1 reinoud
237 1.1 reinoud udf_getownership(fdir_node, &fduid, &gdummy);
238 1.1 reinoud udf_getownership(tdir_node, &tduid, &gdummy);
239 1.1 reinoud udf_getownership(f_node, &fuid, &gdummy);
240 1.1 reinoud
241 1.1 reinoud tuid = 0;
242 1.1 reinoud if (t_node)
243 1.1 reinoud udf_getownership(t_node, &tuid, &gdummy);
244 1.1 reinoud
245 1.1 reinoud return genfs_ufslike_rename_check_permitted(cred,
246 1.1 reinoud fdvp, fdmode, fduid,
247 1.1 reinoud fvp, fuid,
248 1.1 reinoud tdvp, tdmode, tduid,
249 1.1 reinoud tvp, tuid);
250 1.1 reinoud }
251 1.1 reinoud
252 1.1 reinoud
253 1.1 reinoud /*
254 1.1 reinoud * udf_gro_remove_check_possible: check whether a remove is possible
255 1.1 reinoud * independent of credentials.
256 1.1 reinoud *
257 1.1 reinoud * XXX could check for special attributes?
258 1.1 reinoud */
259 1.1 reinoud static int
260 1.1 reinoud udf_gro_remove_check_possible(struct mount *mp,
261 1.1 reinoud struct vnode *dvp, struct vnode *vp)
262 1.1 reinoud {
263 1.1 reinoud (void)mp;
264 1.1 reinoud KASSERT(mp != NULL);
265 1.1 reinoud KASSERT(dvp != NULL);
266 1.1 reinoud KASSERT(vp != NULL);
267 1.1 reinoud KASSERT(dvp != vp);
268 1.1 reinoud KASSERT(dvp->v_type == VDIR);
269 1.1 reinoud KASSERT(vp->v_type != VDIR);
270 1.1 reinoud KASSERT(dvp->v_mount == mp);
271 1.1 reinoud KASSERT(vp->v_mount == mp);
272 1.1 reinoud KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
273 1.1 reinoud KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
274 1.1 reinoud
275 1.1 reinoud DPRINTF(CALL, ("udf_gro_remove_check_possible called\n"));
276 1.1 reinoud if (mp->mnt_flag & MNT_RDONLY)
277 1.1 reinoud return EROFS;
278 1.1 reinoud
279 1.1 reinoud /* flags not implemented since they are not defined (yet) in UDF */
280 1.1 reinoud return 0;
281 1.1 reinoud }
282 1.1 reinoud
283 1.1 reinoud
284 1.1 reinoud /*
285 1.1 reinoud * udf_gro_remove_check_permitted: check whether a remove is permitted given
286 1.1 reinoud * our credentials.
287 1.1 reinoud */
288 1.1 reinoud static int
289 1.1 reinoud udf_gro_remove_check_permitted(struct mount *mp, kauth_cred_t cred,
290 1.1 reinoud struct vnode *dvp, struct vnode *vp)
291 1.1 reinoud {
292 1.1 reinoud struct udf_node *dir_node = VTOI(dvp);
293 1.1 reinoud struct udf_node *udf_node = VTOI(vp);
294 1.1 reinoud mode_t dmode;
295 1.1 reinoud uid_t duid, uid;
296 1.1 reinoud gid_t gdummy;
297 1.1 reinoud
298 1.1 reinoud (void)mp;
299 1.1 reinoud KASSERT(mp != NULL);
300 1.1 reinoud KASSERT(dvp != NULL);
301 1.1 reinoud KASSERT(vp != NULL);
302 1.1 reinoud KASSERT(dvp != vp);
303 1.1 reinoud KASSERT(dvp->v_type == VDIR);
304 1.1 reinoud KASSERT(vp->v_type != VDIR);
305 1.1 reinoud KASSERT(dvp->v_mount == mp);
306 1.1 reinoud KASSERT(vp->v_mount == mp);
307 1.1 reinoud KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
308 1.1 reinoud KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
309 1.1 reinoud
310 1.1 reinoud DPRINTF(CALL, ("udf_gro_remove_check_permitted called\n"));
311 1.1 reinoud dmode = udf_getaccessmode(dir_node);
312 1.1 reinoud
313 1.1 reinoud udf_getownership(dir_node, &duid, &gdummy);
314 1.1 reinoud udf_getownership(udf_node, &uid, &gdummy);
315 1.1 reinoud
316 1.1 reinoud return genfs_ufslike_remove_check_permitted(cred,
317 1.1 reinoud dvp, dmode, duid,
318 1.1 reinoud vp, uid);
319 1.1 reinoud }
320 1.1 reinoud
321 1.1 reinoud
322 1.1 reinoud /*
323 1.1 reinoud * udf_gro_rename: actually perform the rename operation.
324 1.1 reinoud */
325 1.1 reinoud static int
326 1.1 reinoud udf_gro_rename(struct mount *mp, kauth_cred_t cred,
327 1.1 reinoud struct vnode *fdvp, struct componentname *fcnp,
328 1.1 reinoud void *fde, struct vnode *fvp,
329 1.1 reinoud struct vnode *tdvp, struct componentname *tcnp,
330 1.1 reinoud void *tde, struct vnode *tvp)
331 1.1 reinoud {
332 1.1 reinoud struct dirent **fdirentp = fde;
333 1.1 reinoud struct dirent **tdirentp = tde;
334 1.1 reinoud struct udf_node *fnode, *fdnode, *tnode, *tdnode;
335 1.1 reinoud struct vattr fvap;
336 1.1 reinoud int error;
337 1.1 reinoud
338 1.1 reinoud (void)cred;
339 1.1 reinoud KASSERT(mp != NULL);
340 1.1 reinoud KASSERT(fdvp != NULL);
341 1.1 reinoud KASSERT(fcnp != NULL);
342 1.1 reinoud KASSERT(fvp != NULL);
343 1.1 reinoud KASSERT(tdvp != NULL);
344 1.1 reinoud KASSERT(tcnp != NULL);
345 1.1 reinoud KASSERT(fdirentp != NULL);
346 1.1 reinoud KASSERT(tdirentp != NULL);
347 1.1 reinoud KASSERT(fdirentp != tdirentp);
348 1.1 reinoud KASSERT((*fdirentp) != (*tdirentp));
349 1.1 reinoud KASSERT((*fdirentp) != NULL);
350 1.1 reinoud KASSERT(fdvp != fvp);
351 1.1 reinoud KASSERT(fdvp != tvp);
352 1.1 reinoud KASSERT(tdvp != fvp);
353 1.1 reinoud KASSERT(tdvp != tvp);
354 1.1 reinoud KASSERT(fvp != tvp);
355 1.1 reinoud KASSERT(fdvp->v_mount == mp);
356 1.1 reinoud KASSERT(fvp->v_mount == mp);
357 1.1 reinoud KASSERT(tdvp->v_mount == mp);
358 1.1 reinoud KASSERT((tvp == NULL) || (tvp->v_mount == mp));
359 1.1 reinoud KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
360 1.1 reinoud KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
361 1.1 reinoud KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
362 1.1 reinoud KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
363 1.1 reinoud
364 1.1 reinoud DPRINTF(CALL, ("udf_gro_rename called\n"));
365 1.1 reinoud DPRINTF(NODE, ("udf_gro_rename called : %s -> %s\n",
366 1.1 reinoud fcnp->cn_nameptr, tcnp->cn_nameptr));
367 1.1 reinoud
368 1.1 reinoud fnode = VTOI(fvp);
369 1.1 reinoud fdnode = VTOI(fdvp);
370 1.1 reinoud tnode = (tvp == NULL) ? NULL : VTOI(tvp);
371 1.1 reinoud tdnode = VTOI(tdvp);
372 1.1 reinoud
373 1.1 reinoud /* get attribute information */
374 1.1 reinoud error = VOP_GETATTR(fvp, &fvap, NULL);
375 1.1 reinoud if (error)
376 1.1 reinoud return error;
377 1.1 reinoud
378 1.1 reinoud /* remove existing entry if present */
379 1.1 reinoud if (tvp)
380 1.1 reinoud udf_dir_detach(tdnode->ump, tdnode, tnode, tcnp);
381 1.1 reinoud
382 1.1 reinoud /* create new directory entry for the node */
383 1.1 reinoud error = udf_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp);
384 1.1 reinoud if (error)
385 1.1 reinoud return error;
386 1.1 reinoud
387 1.1 reinoud /* unlink old directory entry for the node, if failing, unattach new */
388 1.1 reinoud error = udf_dir_detach(tdnode->ump, fdnode, fnode, fcnp);
389 1.1 reinoud if (error) {
390 1.1 reinoud udf_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
391 1.1 reinoud } else if ((fdnode != tdnode) && (fvp->v_type == VDIR)) {
392 1.1 reinoud /* update fnode's '..' entry */
393 1.1 reinoud error = udf_dir_update_rootentry(fnode->ump, fnode, tdnode);
394 1.1 reinoud if (error) {
395 1.1 reinoud /* 'try' to recover from this situation */
396 1.1 reinoud udf_dir_attach(tdnode->ump, fdnode, fnode, &fvap, fcnp);
397 1.1 reinoud udf_dir_detach(tdnode->ump, tdnode, fnode, tcnp);
398 1.1 reinoud }
399 1.1 reinoud }
400 1.1 reinoud
401 1.1 reinoud if (!error) {
402 1.1 reinoud VN_KNOTE(fvp, NOTE_RENAME);
403 1.1 reinoud genfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
404 1.1 reinoud }
405 1.1 reinoud
406 1.1 reinoud return error;
407 1.1 reinoud }
408 1.1 reinoud
409 1.1 reinoud
410 1.1 reinoud /*
411 1.1 reinoud * udf_gro_remove: rename an object over another link to itself, effectively
412 1.1 reinoud * removing just the original link.
413 1.1 reinoud */
414 1.1 reinoud static int
415 1.1 reinoud udf_gro_remove(struct mount *mp, kauth_cred_t cred,
416 1.1 reinoud struct vnode *dvp, struct componentname *cnp, void *de, struct vnode *vp)
417 1.1 reinoud {
418 1.1 reinoud struct udf_node *dir_node, *udf_node;
419 1.1 reinoud
420 1.1 reinoud (void)vp;
421 1.1 reinoud KASSERT(mp != NULL);
422 1.1 reinoud KASSERT(dvp != NULL);
423 1.1 reinoud KASSERT(cnp != NULL);
424 1.1 reinoud KASSERT(vp != NULL);
425 1.1 reinoud KASSERT(dvp != vp);
426 1.1 reinoud KASSERT(dvp->v_mount == mp);
427 1.1 reinoud KASSERT(vp->v_mount == mp);
428 1.1 reinoud KASSERT(dvp->v_type == VDIR);
429 1.1 reinoud KASSERT(vp->v_type != VDIR);
430 1.1 reinoud KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
431 1.1 reinoud KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
432 1.1 reinoud
433 1.1 reinoud DPRINTF(CALL, ("udf_gro_remove called\n"));
434 1.1 reinoud
435 1.1 reinoud dir_node = VTOI(dvp);
436 1.1 reinoud udf_node = VTOI(vp);
437 1.1 reinoud udf_dir_detach(dir_node->ump, dir_node, udf_node, cnp);
438 1.1 reinoud
439 1.1 reinoud return 0;
440 1.1 reinoud }
441 1.1 reinoud
442 1.1 reinoud
443 1.1 reinoud /*
444 1.1 reinoud * udf_gro_lookup: look up and save the lookup results.
445 1.1 reinoud */
446 1.1 reinoud static int
447 1.1 reinoud udf_gro_lookup(struct mount *mp, struct vnode *dvp,
448 1.1 reinoud struct componentname *cnp, void *de_ret, struct vnode **vp_ret)
449 1.1 reinoud {
450 1.1 reinoud struct udf_node *dir_node, *res_node;
451 1.1 reinoud struct long_ad icb_loc;
452 1.1 reinoud const char *name;
453 1.1 reinoud int namelen, error;
454 1.1 reinoud int found;
455 1.1 reinoud
456 1.1 reinoud (void)mp;
457 1.1 reinoud KASSERT(mp != NULL);
458 1.1 reinoud KASSERT(dvp != NULL);
459 1.1 reinoud KASSERT(cnp != NULL);
460 1.1 reinoud KASSERT(vp_ret != NULL);
461 1.1 reinoud KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
462 1.1 reinoud
463 1.1 reinoud dir_node = VTOI(dvp);
464 1.1 reinoud
465 1.1 reinoud DPRINTF(CALL, ("udf_gro_lookup called\n"));
466 1.1 reinoud
467 1.1 reinoud /* lookup filename in the directory; location icb_loc */
468 1.1 reinoud name = cnp->cn_nameptr;
469 1.1 reinoud namelen = cnp->cn_namelen;
470 1.1 reinoud error = udf_lookup_name_in_dir(dvp, name, namelen,
471 1.1 reinoud &icb_loc, &found);
472 1.1 reinoud if (error)
473 1.1 reinoud return error;
474 1.1 reinoud if (!found)
475 1.1 reinoud return ENOENT;
476 1.1 reinoud
477 1.1 reinoud DPRINTF(LOOKUP, ("udf_gro_lookup found '%s'\n", name));
478 1.1 reinoud error = udf_get_node(dir_node->ump, &icb_loc, &res_node);
479 1.1 reinoud if (error)
480 1.1 reinoud return error;
481 1.1 reinoud *vp_ret = res_node->vnode;
482 1.1 reinoud VOP_UNLOCK(res_node->vnode);
483 1.1 reinoud
484 1.1 reinoud return 0;
485 1.1 reinoud }
486 1.1 reinoud
487 1.1 reinoud
488 1.1 reinoud /*
489 1.1 reinoud * udf_rmdired_p: check whether the directory vp has been rmdired.
490 1.1 reinoud *
491 1.1 reinoud * vp must be locked and referenced.
492 1.1 reinoud */
493 1.1 reinoud static bool
494 1.1 reinoud udf_rmdired_p(struct vnode *vp)
495 1.1 reinoud {
496 1.1 reinoud struct long_ad icb_loc;
497 1.1 reinoud const char *name;
498 1.1 reinoud int namelen;
499 1.1 reinoud int error, found;
500 1.1 reinoud
501 1.1 reinoud DPRINTF(CALL, ("udf_rmdired_p called\n"));
502 1.1 reinoud
503 1.1 reinoud KASSERT(vp != NULL);
504 1.1 reinoud KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
505 1.1 reinoud KASSERT(vp->v_type == VDIR);
506 1.1 reinoud
507 1.1 reinoud /* get our node */
508 1.1 reinoud name = "..";
509 1.1 reinoud namelen = 2;
510 1.1 reinoud error = udf_lookup_name_in_dir(vp, name, namelen,
511 1.1 reinoud &icb_loc, &found);
512 1.1 reinoud return (error || !found);
513 1.1 reinoud }
514 1.1 reinoud
515 1.1 reinoud
516 1.1 reinoud /*
517 1.1 reinoud * udf_gro_genealogy: analyze the genealogy of the source and target
518 1.1 reinoud * directories.
519 1.1 reinoud */
520 1.1 reinoud static int
521 1.1 reinoud udf_gro_genealogy(struct mount *mp, kauth_cred_t cred,
522 1.1 reinoud struct vnode *fdvp, struct vnode *tdvp,
523 1.1 reinoud struct vnode **intermediate_node_ret)
524 1.1 reinoud {
525 1.1 reinoud struct udf_mount *ump;
526 1.1 reinoud struct udf_node *source, *target, *parent_node, *child_node;
527 1.1 reinoud struct long_ad parent_loc;
528 1.1 reinoud const char *name;
529 1.1 reinoud int namelen;
530 1.1 reinoud int error, found;
531 1.1 reinoud
532 1.1 reinoud (void)cred;
533 1.1 reinoud KASSERT(mp != NULL);
534 1.1 reinoud KASSERT(fdvp != NULL);
535 1.1 reinoud KASSERT(tdvp != NULL);
536 1.1 reinoud KASSERT(fdvp != tdvp);
537 1.1 reinoud KASSERT(intermediate_node_ret != NULL);
538 1.1 reinoud KASSERT(fdvp->v_mount == mp);
539 1.1 reinoud KASSERT(tdvp->v_mount == mp);
540 1.1 reinoud KASSERT(fdvp->v_type == VDIR);
541 1.1 reinoud KASSERT(tdvp->v_type == VDIR);
542 1.1 reinoud
543 1.1 reinoud DPRINTF(CALL, ("udf_gro_genealogy called\n"));
544 1.1 reinoud
545 1.1 reinoud /*
546 1.1 reinoud * We need to provisionally lock tdvp to keep rmdir from deleting it
547 1.1 reinoud * -- or any ancestor -- at an inopportune moment.
548 1.1 reinoud *
549 1.1 reinoud * XXX WHY is this not in genfs's rename? XXX
550 1.1 reinoud */
551 1.1 reinoud error = udf_gro_lock_directory(mp, tdvp);
552 1.1 reinoud if (error)
553 1.1 reinoud return error;
554 1.1 reinoud
555 1.1 reinoud source = VTOI(fdvp);
556 1.1 reinoud target = VTOI(tdvp);
557 1.1 reinoud
558 1.1 reinoud name = "..";
559 1.1 reinoud namelen = 2;
560 1.1 reinoud error = 0;
561 1.1 reinoud
562 1.1 reinoud ump = target->ump;
563 1.1 reinoud
564 1.1 reinoud /* if nodes are equal, it is no use looking */
565 1.1 reinoud if (udf_compare_icb(&source->loc, &target->loc) == 0)
566 1.1 reinoud return EEXIST;
567 1.1 reinoud
568 1.1 reinoud child_node = target;
569 1.1 reinoud vref(child_node->vnode);
570 1.1 reinoud
571 1.1 reinoud for (;;) {
572 1.1 reinoud DPRINTF(NODE, ("udf_gro_genealogy : "
573 1.1 reinoud "source vp %p, looking at vp %p\n",
574 1.1 reinoud source->vnode, child_node->vnode));
575 1.1 reinoud
576 1.1 reinoud /* sanity check */
577 1.1 reinoud if (child_node->vnode->v_type != VDIR) {
578 1.1 reinoud vput(child_node->vnode);
579 1.1 reinoud return ENOTDIR;
580 1.1 reinoud }
581 1.1 reinoud
582 1.1 reinoud /* go down one level */
583 1.1 reinoud error = udf_lookup_name_in_dir(child_node->vnode, name, namelen,
584 1.1 reinoud &parent_loc, &found);
585 1.1 reinoud DPRINTF(NODE, ("\tlookup of parent '..' resulted in error %d, "
586 1.1 reinoud "found %d\n", error, found));
587 1.1 reinoud if (!found)
588 1.1 reinoud error = ENOENT;
589 1.1 reinoud if (error) {
590 1.1 reinoud vput(child_node->vnode);
591 1.1 reinoud return error;
592 1.1 reinoud }
593 1.1 reinoud
594 1.1 reinoud /* did we encounter the root node? i.e. loop back */
595 1.1 reinoud if (udf_compare_icb(&parent_loc, &child_node->loc) == 0) {
596 1.1 reinoud DPRINTF(NODE, ("ROOT found!\n"));
597 1.1 reinoud vput(child_node->vnode);
598 1.1 reinoud *intermediate_node_ret = NULL;
599 1.1 reinoud return 0;
600 1.1 reinoud }
601 1.1 reinoud
602 1.1 reinoud /* did we encounter source node? */
603 1.1 reinoud if (udf_compare_icb(&parent_loc, &source->loc) == 0) {
604 1.1 reinoud DPRINTF(NODE, ("SOURCE NODE FOUND\n"));
605 1.1 reinoud *intermediate_node_ret = child_node->vnode;
606 1.1 reinoud VOP_UNLOCK(child_node->vnode);
607 1.1 reinoud return 0;
608 1.1 reinoud }
609 1.1 reinoud
610 1.1 reinoud DPRINTF(NODE, ("\tgetting the parent node\n"));
611 1.1 reinoud error = udf_get_node(ump, &parent_loc, &parent_node);
612 1.1 reinoud if (error) {
613 1.1 reinoud vput(child_node->vnode);
614 1.1 reinoud return error;
615 1.1 reinoud }
616 1.1 reinoud
617 1.1 reinoud if (udf_rmdired_p(parent_node->vnode)) {
618 1.1 reinoud vput(child_node->vnode);
619 1.1 reinoud vput(parent_node->vnode);
620 1.1 reinoud return ENOENT;
621 1.1 reinoud }
622 1.1 reinoud
623 1.1 reinoud /*
624 1.1 reinoud * Push our intermediate node, we're done with it, but do
625 1.1 reinoud * remember our parent location
626 1.1 reinoud */
627 1.1 reinoud vput(child_node->vnode);
628 1.1 reinoud child_node = parent_node;
629 1.1 reinoud }
630 1.1 reinoud }
631 1.1 reinoud
632 1.1 reinoud
633 1.1 reinoud /*
634 1.1 reinoud * udf_gro_lock_directory: lock the directory vp, but fail if it has been
635 1.1 reinoud * rmdir'd.
636 1.1 reinoud */
637 1.1 reinoud static int
638 1.1 reinoud udf_gro_lock_directory(struct mount *mp, struct vnode *vp)
639 1.1 reinoud {
640 1.1 reinoud
641 1.1 reinoud (void)mp;
642 1.1 reinoud KASSERT(mp != NULL);
643 1.1 reinoud KASSERT(vp != NULL);
644 1.1 reinoud KASSERT(vp->v_mount == mp);
645 1.1 reinoud
646 1.1 reinoud DPRINTF(CALL, ("udf_gro_lock_directory called\n"));
647 1.1 reinoud DPRINTF(LOCKING, ("udf_gro_lock_directory called\n"));
648 1.1 reinoud
649 1.1 reinoud vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
650 1.1 reinoud
651 1.1 reinoud if (udf_rmdired_p(vp)) {
652 1.1 reinoud VOP_UNLOCK(vp);
653 1.1 reinoud return ENOENT;
654 1.1 reinoud }
655 1.1 reinoud
656 1.1 reinoud return 0;
657 1.1 reinoud }
658 1.1 reinoud
659 1.1 reinoud
660 1.1 reinoud static const struct genfs_rename_ops udf_genfs_rename_ops = {
661 1.1 reinoud .gro_directory_empty_p = udf_gro_directory_empty_p,
662 1.1 reinoud .gro_rename_check_possible = udf_gro_rename_check_possible,
663 1.1 reinoud .gro_rename_check_permitted = udf_gro_rename_check_permitted,
664 1.1 reinoud .gro_remove_check_possible = udf_gro_remove_check_possible,
665 1.1 reinoud .gro_remove_check_permitted = udf_gro_remove_check_permitted,
666 1.1 reinoud .gro_rename = udf_gro_rename,
667 1.1 reinoud .gro_remove = udf_gro_remove,
668 1.1 reinoud .gro_lookup = udf_gro_lookup,
669 1.1 reinoud .gro_genealogy = udf_gro_genealogy,
670 1.1 reinoud .gro_lock_directory = udf_gro_lock_directory,
671 1.1 reinoud };
672