Annotation of src/sys/ufs/ufs/ufs_rename.c, Revision 1.9
1.9 ! christos 1: /* $NetBSD: ufs_rename.c,v 1.8 2013/06/19 17:51:26 dholland Exp $ */
1.1 riastrad 2:
3: /*-
4: * Copyright (c) 2012 The NetBSD Foundation, Inc.
5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
8: * by Taylor R Campbell.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29: * POSSIBILITY OF SUCH DAMAGE.
30: */
31:
32: /*
33: * UFS Rename
34: */
35:
36: #include <sys/cdefs.h>
1.9 ! christos 37: __KERNEL_RCSID(0, "$NetBSD: ufs_rename.c,v 1.8 2013/06/19 17:51:26 dholland Exp $");
1.1 riastrad 38:
39: #include <sys/param.h>
40: #include <sys/buf.h>
41: #include <sys/errno.h>
42: #include <sys/fstrans.h>
43: #include <sys/kauth.h>
44: #include <sys/mount.h>
45: #include <sys/namei.h>
46: #include <sys/pool.h>
47: #include <sys/vnode.h>
48: #include <sys/vnode_if.h>
49: #include <sys/wapbl.h>
50:
51: #include <miscfs/genfs/genfs.h>
52:
53: #include <ufs/ufs/dir.h>
54: #include <ufs/ufs/inode.h>
55: #include <ufs/ufs/ufs_bswap.h>
56: #include <ufs/ufs/ufs_extern.h>
57: #include <ufs/ufs/ufs_wapbl.h>
58: #include <ufs/ufs/ufsmount.h>
59:
60: /*
61: * Forward declarations
62: */
63:
64: static int ufs_sane_rename(struct vnode *, struct componentname *,
65: struct vnode *, struct componentname *,
66: kauth_cred_t, bool);
67: static bool ufs_rename_ulr_overlap_p(const struct ufs_lookup_results *,
68: const struct ufs_lookup_results *);
69: static int ufs_rename_recalculate_fulr(struct vnode *,
70: struct ufs_lookup_results *, const struct ufs_lookup_results *,
71: const struct componentname *);
72: static int ufs_direct_namlen(const struct direct *, const struct vnode *);
73: static int ufs_read_dotdot(struct vnode *, kauth_cred_t, ino_t *);
74: static int ufs_dirbuf_dotdot_namlen(const struct dirtemplate *,
75: const struct vnode *);
76:
77: static const struct genfs_rename_ops ufs_genfs_rename_ops;
78:
79: /*
80: * ufs_sane_rename: The hairiest vop, with the saner API.
81: *
82: * Arguments:
83: *
84: * . fdvp (from directory vnode),
85: * . fcnp (from component name),
86: * . tdvp (to directory vnode),
87: * . tcnp (to component name),
88: * . cred (credentials structure), and
89: * . posixly_correct (flag for behaviour if target & source link same file).
90: *
91: * fdvp and tdvp may be the same, and must be referenced and unlocked.
92: */
93: static int
94: ufs_sane_rename(
95: struct vnode *fdvp, struct componentname *fcnp,
96: struct vnode *tdvp, struct componentname *tcnp,
97: kauth_cred_t cred, bool posixly_correct)
98: {
99: struct ufs_lookup_results fulr, tulr;
100:
101: return genfs_sane_rename(&ufs_genfs_rename_ops,
102: fdvp, fcnp, &fulr, tdvp, tcnp, &tulr,
103: cred, posixly_correct);
104: }
105:
106: /*
107: * ufs_rename: The hairiest vop, with the insanest API. Defer to
108: * genfs_insane_rename immediately.
109: */
110: int
111: ufs_rename(void *v)
112: {
113:
114: return genfs_insane_rename(v, &ufs_sane_rename);
115: }
116:
117: /*
118: * ufs_gro_directory_empty_p: Return true if the directory vp is
119: * empty. dvp is its parent.
120: *
121: * vp and dvp must be locked and referenced.
122: */
123: bool
124: ufs_gro_directory_empty_p(struct mount *mp, kauth_cred_t cred,
125: struct vnode *vp, struct vnode *dvp)
126: {
127:
128: (void)mp;
129: KASSERT(mp != NULL);
130: KASSERT(vp != NULL);
131: KASSERT(dvp != NULL);
132: KASSERT(vp != dvp);
133: KASSERT(vp->v_mount == mp);
134: KASSERT(dvp->v_mount == mp);
135: KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
136: KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
137:
138: return ufs_dirempty(VTOI(vp), VTOI(dvp)->i_number, cred);
139: }
140:
141: /*
142: * ufs_gro_rename_check_possible: Check whether a rename is possible
143: * independent of credentials.
144: */
145: int
146: ufs_gro_rename_check_possible(struct mount *mp,
147: struct vnode *fdvp, struct vnode *fvp,
148: struct vnode *tdvp, struct vnode *tvp)
149: {
150:
151: (void)mp;
152: KASSERT(mp != NULL);
153: KASSERT(fdvp != NULL);
154: KASSERT(fvp != NULL);
155: KASSERT(tdvp != NULL);
156: KASSERT(fdvp != fvp);
157: KASSERT(fdvp != tvp);
158: KASSERT(tdvp != fvp);
159: KASSERT(tdvp != tvp);
160: KASSERT(fvp != tvp);
161: KASSERT(fdvp->v_type == VDIR);
162: KASSERT(tdvp->v_type == VDIR);
163: KASSERT(fdvp->v_mount == mp);
164: KASSERT(fvp->v_mount == mp);
165: KASSERT(tdvp->v_mount == mp);
166: KASSERT((tvp == NULL) || (tvp->v_mount == mp));
167: KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
168: KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
169: KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
170: KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
171:
172: return genfs_ufslike_rename_check_possible(
173: VTOI(fdvp)->i_flags, VTOI(fvp)->i_flags,
174: VTOI(tdvp)->i_flags, (tvp? VTOI(tvp)->i_flags : 0),
175: (tvp != NULL),
176: IMMUTABLE, APPEND);
177: }
178:
179: /*
180: * ufs_gro_rename_check_permitted: Check whether a rename is permitted
181: * given our credentials.
182: */
183: int
184: ufs_gro_rename_check_permitted(struct mount *mp, kauth_cred_t cred,
185: struct vnode *fdvp, struct vnode *fvp,
186: struct vnode *tdvp, struct vnode *tvp)
187: {
188:
189: (void)mp;
190: KASSERT(mp != NULL);
191: KASSERT(fdvp != NULL);
192: KASSERT(fvp != NULL);
193: KASSERT(tdvp != NULL);
194: KASSERT(fdvp != fvp);
195: KASSERT(fdvp != tvp);
196: KASSERT(tdvp != fvp);
197: KASSERT(tdvp != tvp);
198: KASSERT(fvp != tvp);
199: KASSERT(fdvp->v_type == VDIR);
200: KASSERT(tdvp->v_type == VDIR);
201: KASSERT(fdvp->v_mount == mp);
202: KASSERT(fvp->v_mount == mp);
203: KASSERT(tdvp->v_mount == mp);
204: KASSERT((tvp == NULL) || (tvp->v_mount == mp));
205: KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
206: KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
207: KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
208: KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
209:
210: return genfs_ufslike_rename_check_permitted(cred,
211: fdvp, VTOI(fdvp)->i_mode, VTOI(fdvp)->i_uid,
212: fvp, VTOI(fvp)->i_uid,
213: tdvp, VTOI(tdvp)->i_mode, VTOI(tdvp)->i_uid,
214: tvp, (tvp? VTOI(tvp)->i_uid : 0));
215: }
216:
217: /*
218: * ufs_gro_remove_check_possible: Check whether a remove is possible
219: * independent of credentials.
220: */
221: int
222: ufs_gro_remove_check_possible(struct mount *mp,
223: struct vnode *dvp, struct vnode *vp)
224: {
225:
226: (void)mp;
227: KASSERT(mp != NULL);
228: KASSERT(dvp != NULL);
229: KASSERT(vp != NULL);
230: KASSERT(dvp != vp);
231: KASSERT(dvp->v_type == VDIR);
232: KASSERT(vp->v_type != VDIR);
233: KASSERT(dvp->v_mount == mp);
234: KASSERT(vp->v_mount == mp);
235: KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
236: KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
237:
238: return genfs_ufslike_remove_check_possible(
239: VTOI(dvp)->i_flags, VTOI(vp)->i_flags,
240: IMMUTABLE, APPEND);
241: }
242:
243: /*
244: * ufs_gro_remove_check_permitted: Check whether a remove is permitted
245: * given our credentials.
246: */
247: int
248: ufs_gro_remove_check_permitted(struct mount *mp, kauth_cred_t cred,
249: struct vnode *dvp, struct vnode *vp)
250: {
251:
252: (void)mp;
253: KASSERT(mp != NULL);
254: KASSERT(dvp != NULL);
255: KASSERT(vp != NULL);
256: KASSERT(dvp != vp);
257: KASSERT(dvp->v_type == VDIR);
258: KASSERT(vp->v_type != VDIR);
259: KASSERT(dvp->v_mount == mp);
260: KASSERT(vp->v_mount == mp);
261: KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
262: KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
263:
264: return genfs_ufslike_remove_check_permitted(cred,
265: dvp, VTOI(dvp)->i_mode, VTOI(dvp)->i_uid, vp, VTOI(vp)->i_uid);
266: }
267:
268: /*
269: * A virgin directory (no blushing please).
270: *
271: * XXX Copypasta from ufs_vnops.c. Kill!
272: */
273: static const struct dirtemplate mastertemplate = {
1.7 dholland 274: 0, 12, DT_DIR, 1, ".",
275: 0, UFS_DIRBLKSIZ - 12, DT_DIR, 2, ".."
1.1 riastrad 276: };
277:
278: /*
279: * ufs_gro_rename: Actually perform the rename operation.
280: */
281: int
282: ufs_gro_rename(struct mount *mp, kauth_cred_t cred,
283: struct vnode *fdvp, struct componentname *fcnp,
284: void *fde, struct vnode *fvp,
285: struct vnode *tdvp, struct componentname *tcnp,
286: void *tde, struct vnode *tvp)
287: {
288: struct ufs_lookup_results *fulr = fde;
289: struct ufs_lookup_results *tulr = tde;
290: bool directory_p, reparent_p;
291: struct direct *newdir;
292: int error;
293:
294: KASSERT(mp != NULL);
295: KASSERT(fdvp != NULL);
296: KASSERT(fcnp != NULL);
297: KASSERT(fulr != NULL);
298: KASSERT(fvp != NULL);
299: KASSERT(tdvp != NULL);
300: KASSERT(tcnp != NULL);
301: KASSERT(tulr != NULL);
302: KASSERT(fulr != tulr);
303: KASSERT(fdvp != fvp);
304: KASSERT(fdvp != tvp);
305: KASSERT(tdvp != fvp);
306: KASSERT(tdvp != tvp);
307: KASSERT(fvp != tvp);
308: KASSERT(fdvp->v_mount == mp);
309: KASSERT(fvp->v_mount == mp);
310: KASSERT(tdvp->v_mount == mp);
311: KASSERT((tvp == NULL) || (tvp->v_mount == mp));
312: KASSERT(VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE);
313: KASSERT(VOP_ISLOCKED(fvp) == LK_EXCLUSIVE);
314: KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
315: KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
316:
317: /*
318: * We shall need to temporarily bump the link count, so make
319: * sure there is room to do so.
320: */
321: if ((nlink_t)VTOI(fvp)->i_nlink >= LINK_MAX)
322: return EMLINK;
323:
324: directory_p = (fvp->v_type == VDIR);
325: KASSERT(directory_p == ((VTOI(fvp)->i_mode & IFMT) == IFDIR));
326: KASSERT((tvp == NULL) || (directory_p == (tvp->v_type == VDIR)));
327: KASSERT((tvp == NULL) || (directory_p ==
328: ((VTOI(tvp)->i_mode & IFMT) == IFDIR)));
329:
330: reparent_p = (fdvp != tdvp);
331: KASSERT(reparent_p == (VTOI(fdvp)->i_number != VTOI(tdvp)->i_number));
332:
333: /*
334: * Commence hacking of the data on disk.
335: */
336:
337: fstrans_start(mp, FSTRANS_SHARED);
338: error = UFS_WAPBL_BEGIN(mp);
339: if (error)
340: goto ihateyou;
341:
342: /*
343: * 1) Bump link count while we're moving stuff
344: * around. If we crash somewhere before
345: * completing our work, the link count
346: * may be wrong, but correctable.
347: */
348:
349: KASSERT((nlink_t)VTOI(fvp)->i_nlink < LINK_MAX);
350: VTOI(fvp)->i_nlink++;
351: DIP_ASSIGN(VTOI(fvp), nlink, VTOI(fvp)->i_nlink);
352: VTOI(fvp)->i_flag |= IN_CHANGE;
353: error = UFS_UPDATE(fvp, NULL, NULL, UPDATE_DIROP);
354: if (error)
355: goto whymustithurtsomuch;
356:
357: /*
358: * 2) If target doesn't exist, link the target
359: * to the source and unlink the source.
360: * Otherwise, rewrite the target directory
361: * entry to reference the source inode and
362: * expunge the original entry's existence.
363: */
364:
365: if (tvp == NULL) {
366: /*
367: * Account for ".." in new directory.
368: * When source and destination have the same
369: * parent we don't fool with the link count.
370: */
371: if (directory_p && reparent_p) {
372: if ((nlink_t)VTOI(tdvp)->i_nlink >= LINK_MAX) {
373: error = EMLINK;
374: goto whymustithurtsomuch;
375: }
376: KASSERT((nlink_t)VTOI(tdvp)->i_nlink < LINK_MAX);
377: VTOI(tdvp)->i_nlink++;
378: DIP_ASSIGN(VTOI(tdvp), nlink, VTOI(tdvp)->i_nlink);
379: VTOI(tdvp)->i_flag |= IN_CHANGE;
380: error = UFS_UPDATE(tdvp, NULL, NULL, UPDATE_DIROP);
381: if (error) {
382: /*
383: * Link count update didn't take --
384: * back out the in-memory link count.
385: */
386: KASSERT(0 < VTOI(tdvp)->i_nlink);
387: VTOI(tdvp)->i_nlink--;
388: DIP_ASSIGN(VTOI(tdvp), nlink,
389: VTOI(tdvp)->i_nlink);
390: VTOI(tdvp)->i_flag |= IN_CHANGE;
391: goto whymustithurtsomuch;
392: }
393: }
394:
395: newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK);
396: ufs_makedirentry(VTOI(fvp), tcnp, newdir);
397: error = ufs_direnter(tdvp, tulr, NULL, newdir, tcnp, NULL);
398: pool_cache_put(ufs_direct_cache, newdir);
399: if (error) {
400: if (directory_p && reparent_p) {
401: /*
402: * Directory update didn't take, but
403: * the link count update did -- back
404: * out the in-memory link count and the
405: * on-disk link count.
406: */
407: KASSERT(0 < VTOI(tdvp)->i_nlink);
408: VTOI(tdvp)->i_nlink--;
409: DIP_ASSIGN(VTOI(tdvp), nlink,
410: VTOI(tdvp)->i_nlink);
411: VTOI(tdvp)->i_flag |= IN_CHANGE;
412: (void)UFS_UPDATE(tdvp, NULL, NULL,
413: UPDATE_WAIT | UPDATE_DIROP);
414: }
415: goto whymustithurtsomuch;
416: }
417: } else {
418: if (directory_p)
419: /* XXX WTF? Why purge here? Why not purge others? */
420: cache_purge(tdvp);
421:
422: /*
423: * Make the target directory's entry for tcnp point at
424: * the source node.
425: *
426: * XXX ufs_dirrewrite decrements tvp's link count, but
427: * doesn't touch the link count of the new inode. Go
428: * figure.
429: */
430: error = ufs_dirrewrite(VTOI(tdvp), tulr->ulr_offset,
431: VTOI(tvp), VTOI(fvp)->i_number, IFTODT(VTOI(fvp)->i_mode),
432: ((directory_p && reparent_p) ? reparent_p : directory_p),
433: IN_CHANGE | IN_UPDATE);
434: if (error)
435: goto whymustithurtsomuch;
436:
437: /*
438: * If the source and target are directories, and the
439: * target is in the same directory as the source,
440: * decrement the link count of the common parent
441: * directory, since we are removing the target from
442: * that directory.
443: */
444: if (directory_p && !reparent_p) {
445: KASSERT(fdvp == tdvp);
446: /* XXX check, don't kassert */
447: KASSERT(0 < VTOI(tdvp)->i_nlink);
448: VTOI(tdvp)->i_nlink--;
449: DIP_ASSIGN(VTOI(tdvp), nlink, VTOI(tdvp)->i_nlink);
450: VTOI(tdvp)->i_flag |= IN_CHANGE;
451: UFS_WAPBL_UPDATE(tdvp, NULL, NULL, 0);
452: }
453:
454: if (directory_p) {
455: /*
456: * XXX I don't understand the following comment
457: * from ufs_rename -- in particular, the part
458: * about `there may be other hard links'.
459: *
460: * Truncate inode. The only stuff left in the directory
461: * is "." and "..". The "." reference is inconsequential
462: * since we are quashing it. We have removed the "."
463: * reference and the reference in the parent directory,
464: * but there may be other hard links.
465: *
466: * XXX The ufs_dirempty call earlier does
467: * not guarantee anything about nlink.
468: */
469: if (VTOI(tvp)->i_nlink != 1)
470: ufs_dirbad(VTOI(tvp), (doff_t)0,
471: "hard-linked directory");
472: VTOI(tvp)->i_nlink = 0;
473: DIP_ASSIGN(VTOI(tvp), nlink, 0);
474: error = UFS_TRUNCATE(tvp, (off_t)0, IO_SYNC, cred);
475: if (error)
476: goto whymustithurtsomuch;
477: }
478: }
479:
480: /*
481: * If the source is a directory with a new parent, the link
482: * count of the old parent directory must be decremented and
483: * ".." set to point to the new parent.
484: *
485: * XXX ufs_dirrewrite updates the link count of fdvp, but not
486: * the link count of fvp or the link count of tdvp. Go figure.
487: */
488: if (directory_p && reparent_p) {
489: error = ufs_dirrewrite(VTOI(fvp), mastertemplate.dot_reclen,
490: VTOI(fdvp), VTOI(tdvp)->i_number, DT_DIR, 0, IN_CHANGE);
491: #if 0 /* XXX This branch was not in ufs_rename! */
492: if (error)
493: goto whymustithurtsomuch;
494: #endif
495:
496: /* XXX WTF? Why purge here? Why not purge others? */
497: cache_purge(fdvp);
498: }
499:
500: /*
501: * 3) Unlink the source.
502: */
503:
504: /*
505: * ufs_direnter may compact the directory in the process of
506: * inserting a new entry. That may invalidate fulr, which we
507: * need in order to remove the old entry. In that case, we
508: * need to recalculate what fulr should be.
509: */
1.3 riastrad 510: if (!reparent_p && (tvp == NULL) &&
511: ufs_rename_ulr_overlap_p(fulr, tulr)) {
1.1 riastrad 512: error = ufs_rename_recalculate_fulr(fdvp, fulr, tulr, fcnp);
513: #if 0 /* XXX */
514: if (error) /* XXX Try to back out changes? */
515: goto whymustithurtsomuch;
516: #endif
517: }
518:
519: /*
520: * XXX 0 means !isrmdir. But can't this be an rmdir?
521: * XXX Well, turns out that argument to ufs_dirremove is ignored...
522: * XXX And it turns out ufs_dirremove updates the link count of fvp.
523: * XXX But it doesn't update the link count of fdvp. Go figure.
524: * XXX fdvp's link count is updated in ufs_dirrewrite instead.
525: * XXX Actually, sometimes it doesn't update fvp's link count.
526: * XXX I hate the world.
527: */
528: error = ufs_dirremove(fdvp, fulr, VTOI(fvp), fcnp->cn_flags, 0);
529: if (error)
530: #if 0 /* XXX */
531: goto whymustithurtsomuch;
532: #endif
533: goto arghmybrainhurts;
534:
535: /*
536: * XXX Perhaps this should go at the top, in case the file
537: * system is modified but incompletely so because of an
538: * intermediate error.
539: */
540: genfs_rename_knote(fdvp, fvp, tdvp, tvp,
541: ((tvp != NULL) && (VTOI(tvp)->i_nlink == 0)));
542: #if 0 /* XXX */
543: genfs_rename_cache_purge(fdvp, fvp, tdvp, tvp);
544: #endif
545: goto arghmybrainhurts;
546:
547: whymustithurtsomuch:
548: KASSERT(0 < VTOI(fvp)->i_nlink);
549: VTOI(fvp)->i_nlink--;
550: DIP_ASSIGN(VTOI(fvp), nlink, VTOI(fvp)->i_nlink);
551: VTOI(fvp)->i_flag |= IN_CHANGE;
552: UFS_WAPBL_UPDATE(fvp, NULL, NULL, 0);
553:
554: arghmybrainhurts:
555: UFS_WAPBL_END(mp);
556:
557: ihateyou:
558: fstrans_done(mp);
559: return error;
560: }
561:
562: /*
563: * ufs_rename_ulr_overlap_p: True iff tulr overlaps with fulr so that
564: * entering a directory entry at tulr may move fulr.
565: */
566: static bool
567: ufs_rename_ulr_overlap_p(const struct ufs_lookup_results *fulr,
568: const struct ufs_lookup_results *tulr)
569: {
570: doff_t from_prev_start, from_prev_end, to_start, to_end;
571:
572: KASSERT(fulr != NULL);
573: KASSERT(tulr != NULL);
574: KASSERT(fulr != tulr);
575:
576: /*
577: * fulr is from a DELETE lookup, so fulr->ulr_count is the size
578: * of the preceding entry (d_reclen).
579: */
580: from_prev_end = fulr->ulr_offset;
581: KASSERT(fulr->ulr_count <= from_prev_end);
582: from_prev_start = (from_prev_end - fulr->ulr_count);
583:
584: /*
585: * tulr is from a RENAME lookup, so tulr->ulr_count is the size
586: * of the free space for an entry that we are about to fill.
587: */
588: to_start = tulr->ulr_offset;
1.8 dholland 589: KASSERT(tulr->ulr_count < (UFS_MAXDIRSIZE - to_start));
1.1 riastrad 590: to_end = (to_start + tulr->ulr_count);
591:
592: return
593: (((to_start <= from_prev_start) && (from_prev_start < to_end)) ||
594: ((to_start <= from_prev_end) && (from_prev_end < to_end)));
595: }
596:
597: /*
598: * ufs_rename_recalculate_fulr: If we have just entered a directory into
599: * dvp at tulr, and we were about to remove one at fulr for an entry
600: * named fcnp, fulr may be invalid. So, if necessary, recalculate it.
601: */
602: static int
603: ufs_rename_recalculate_fulr(struct vnode *dvp,
604: struct ufs_lookup_results *fulr, const struct ufs_lookup_results *tulr,
605: const struct componentname *fcnp)
606: {
607: struct mount *mp;
608: struct ufsmount *ump;
609: int needswap;
610: /* XXX int is a silly type for this; blame ufsmount::um_dirblksiz. */
1.3 riastrad 611: int dirblksiz;
612: doff_t search_start, search_end;
1.1 riastrad 613: doff_t offset; /* Offset of entry we're examining. */
614: struct buf *bp; /* I/O block we're examining. */
1.3 riastrad 615: char *dirbuf; /* Pointer into directory at search_start. */
1.1 riastrad 616: struct direct *ep; /* Pointer to the entry we're examining. */
617: /* XXX direct::d_reclen is 16-bit;
618: * ufs_lookup_results::ulr_reclen is 32-bit. Blah. */
619: uint32_t reclen; /* Length of the entry we're examining. */
620: uint32_t prev_reclen; /* Length of the preceding entry. */
621: int error;
622:
623: KASSERT(dvp != NULL);
624: KASSERT(dvp->v_mount != NULL);
625: KASSERT(VTOI(dvp) != NULL);
626: KASSERT(fulr != NULL);
627: KASSERT(tulr != NULL);
628: KASSERT(fulr != tulr);
629: KASSERT(ufs_rename_ulr_overlap_p(fulr, tulr));
630:
631: mp = dvp->v_mount;
632: ump = VFSTOUFS(mp);
633: KASSERT(ump != NULL);
634: KASSERT(ump == VTOI(dvp)->i_ump);
635:
636: needswap = UFS_MPNEEDSWAP(ump);
637:
1.3 riastrad 638: dirblksiz = ump->um_dirblksiz;
639: KASSERT(0 < dirblksiz);
640: KASSERT((dirblksiz & (dirblksiz - 1)) == 0);
641:
642: /* A directory block may not span across multiple I/O blocks. */
643: KASSERT(dirblksiz <= mp->mnt_stat.f_iosize);
1.1 riastrad 644:
1.3 riastrad 645: /* Find the bounds of the search. */
646: search_start = tulr->ulr_offset;
1.8 dholland 647: KASSERT(fulr->ulr_reclen < (UFS_MAXDIRSIZE - fulr->ulr_offset));
1.1 riastrad 648: search_end = (fulr->ulr_offset + fulr->ulr_reclen);
649:
1.3 riastrad 650: /* Compaction must happen only within a directory block. (*) */
651: KASSERT(search_start <= search_end);
652: KASSERT((search_end - (search_start &~ (dirblksiz - 1))) <= dirblksiz);
653:
1.1 riastrad 654: dirbuf = NULL;
655: bp = NULL;
1.3 riastrad 656: error = ufs_blkatoff(dvp, (off_t)search_start, &dirbuf, &bp, false);
1.1 riastrad 657: if (error)
658: return error;
659: KASSERT(dirbuf != NULL);
660: KASSERT(bp != NULL);
661:
1.3 riastrad 662: /*
663: * Guarantee we sha'n't go past the end of the buffer we got.
664: * dirbuf is bp->b_data + (search_start & (iosize - 1)), and
1.4 riastrad 665: * the valid range is [bp->b_data, bp->b_data + bp->b_bcount).
1.3 riastrad 666: */
667: KASSERT((search_end - search_start) <=
668: (bp->b_bcount - (search_start & (mp->mnt_stat.f_iosize - 1))));
669:
1.1 riastrad 670: prev_reclen = fulr->ulr_count;
1.3 riastrad 671: offset = search_start;
1.1 riastrad 672:
673: /*
1.3 riastrad 674: * Search from search_start to search_end for the entry matching
1.1 riastrad 675: * fcnp, which must be there because we found it before and it
676: * should only at most have moved earlier.
677: */
678: for (;;) {
1.3 riastrad 679: KASSERT(search_start <= offset);
1.1 riastrad 680: KASSERT(offset < search_end);
681:
682: /*
683: * Examine the directory entry at offset.
684: */
1.3 riastrad 685: ep = (struct direct *)(dirbuf + (offset - search_start));
1.1 riastrad 686: reclen = ufs_rw16(ep->d_reclen, needswap);
687:
688: if (ep->d_ino == 0)
689: goto next; /* Entry is unused. */
690:
1.6 dholland 691: if (ufs_rw32(ep->d_ino, needswap) == UFS_WINO)
1.1 riastrad 692: goto next; /* Entry is whiteout. */
693:
694: if (fcnp->cn_namelen != ufs_direct_namlen(ep, dvp))
695: goto next; /* Wrong name length. */
696:
697: if (memcmp(ep->d_name, fcnp->cn_nameptr, fcnp->cn_namelen))
698: goto next; /* Wrong name. */
699:
700: /* Got it! */
701: break;
702:
703: next:
704: if (! ((reclen < search_end) &&
705: (offset < (search_end - reclen)))) {
706: brelse(bp, 0);
707: return EIO; /* XXX Panic? What? */
708: }
709:
1.3 riastrad 710: /* We may not move past the search end. */
1.1 riastrad 711: KASSERT(reclen < search_end);
712: KASSERT(offset < (search_end - reclen));
1.3 riastrad 713:
714: /*
715: * We may not move across a directory block boundary;
716: * see (*) above.
717: */
718: KASSERT((offset &~ (dirblksiz - 1)) ==
719: ((offset + reclen) &~ (dirblksiz - 1)));
720:
1.1 riastrad 721: prev_reclen = reclen;
722: offset += reclen;
723: }
724:
725: /*
726: * Found the entry. Record where.
727: */
728: fulr->ulr_offset = offset;
729: fulr->ulr_reclen = reclen;
730:
731: /*
732: * Record the preceding record length, but not if we're at the
733: * start of a directory block.
734: */
1.3 riastrad 735: fulr->ulr_count = ((offset & (dirblksiz - 1))? prev_reclen : 0);
1.1 riastrad 736:
737: brelse(bp, 0);
738: return 0;
739: }
740:
741: /*
742: * ufs_direct_namlen: Return the namlen of the directory entry ep from
743: * the directory vp.
744: */
745: static int /* XXX int? uint8_t? */
746: ufs_direct_namlen(const struct direct *ep, const struct vnode *vp)
747: {
748: bool swap;
749:
750: KASSERT(ep != NULL);
751: KASSERT(vp != NULL);
752: KASSERT(VTOI(vp) != NULL);
753: KASSERT(VTOI(vp)->i_ump != NULL);
754:
755: #if (BYTE_ORDER == LITTLE_ENDIAN)
756: swap = (UFS_MPNEEDSWAP(VTOI(vp)->i_ump) == 0);
757: #else
758: swap = (UFS_MPNEEDSWAP(VTOI(vp)->i_ump) != 0);
759: #endif
760:
761: return ((FSFMT(vp) && swap)? ep->d_type : ep->d_namlen);
762: }
763:
764: /*
765: * ufs_gro_remove: Rename an object over another link to itself,
766: * effectively removing just the original link.
767: */
768: int
769: ufs_gro_remove(struct mount *mp, kauth_cred_t cred,
770: struct vnode *dvp, struct componentname *cnp, void *de, struct vnode *vp)
771: {
772: struct ufs_lookup_results *ulr = de;
773: int error;
774:
775: KASSERT(mp != NULL);
776: KASSERT(dvp != NULL);
777: KASSERT(cnp != NULL);
778: KASSERT(ulr != NULL);
779: KASSERT(vp != NULL);
780: KASSERT(dvp != vp);
781: KASSERT(dvp->v_mount == mp);
782: KASSERT(vp->v_mount == mp);
783: KASSERT(dvp->v_type == VDIR);
784: KASSERT(vp->v_type != VDIR);
785: KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
786: KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
787: KASSERT(cnp->cn_nameiop == DELETE);
788:
789: fstrans_start(mp, FSTRANS_SHARED);
790: error = UFS_WAPBL_BEGIN(mp);
791: if (error)
792: goto out0;
793:
794: /* XXX ufs_dirremove decrements vp's link count for us. */
795: error = ufs_dirremove(dvp, ulr, VTOI(vp), cnp->cn_flags, 0);
796: if (error)
797: goto out1;
798:
799: VN_KNOTE(dvp, NOTE_WRITE);
800: VN_KNOTE(vp, (VTOI(vp)->i_nlink? NOTE_LINK : NOTE_DELETE));
801:
802: out1: UFS_WAPBL_END(mp);
803: out0: fstrans_done(mp);
804: return error;
805: }
806:
807: /*
808: * ufs_gro_lookup: Look up and save the lookup results.
809: */
810: int
811: ufs_gro_lookup(struct mount *mp, struct vnode *dvp,
812: struct componentname *cnp, void *de_ret, struct vnode **vp_ret)
813: {
814: struct ufs_lookup_results *ulr_ret = de_ret;
815: struct vnode *vp = NULL;
816: int error;
817:
818: (void)mp;
819: KASSERT(mp != NULL);
820: KASSERT(dvp != NULL);
821: KASSERT(cnp != NULL);
822: KASSERT(ulr_ret != NULL);
823: KASSERT(vp_ret != NULL);
824: KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
825:
826: /* Kludge cargo-culted from dholland's ufs_rename. */
827: cnp->cn_flags &=~ MODMASK;
828: cnp->cn_flags |= (LOCKPARENT | LOCKLEAF);
829:
830: error = relookup(dvp, &vp, cnp, 0 /* dummy */);
831: if ((error == 0) && (vp == NULL)) {
832: error = ENOENT;
833: goto out;
834: } else if (error) {
835: return error;
836: }
837:
838: /*
839: * Thanks to VFS insanity, relookup locks vp, which screws us
840: * in various ways.
841: */
842: KASSERT(vp != NULL);
843: VOP_UNLOCK(vp);
844:
845: out: *ulr_ret = VTOI(dvp)->i_crap;
846: *vp_ret = vp;
847: return error;
848: }
849:
850: /*
851: * ufs_rmdired_p: Check whether the directory vp has been rmdired.
852: *
853: * vp must be locked and referenced.
854: */
855: static bool
856: ufs_rmdired_p(struct vnode *vp)
857: {
858:
859: KASSERT(vp != NULL);
860: KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
861: KASSERT(vp->v_type == VDIR);
862:
863: /* XXX Is this correct? */
864: return (VTOI(vp)->i_size == 0);
865: }
866:
867: /*
868: * ufs_read_dotdot: Store in *ino_ret the inode number of the parent
869: * of the directory vp.
870: */
871: static int
872: ufs_read_dotdot(struct vnode *vp, kauth_cred_t cred, ino_t *ino_ret)
873: {
874: struct dirtemplate dirbuf;
875: int error;
876:
877: KASSERT(vp != NULL);
878: KASSERT(ino_ret != NULL);
879: KASSERT(vp->v_type == VDIR);
880:
881: error = vn_rdwr(UIO_READ, vp, &dirbuf, sizeof dirbuf, (off_t)0,
882: UIO_SYSSPACE, IO_NODELOCKED, cred, NULL, NULL);
883: if (error)
884: return error;
885:
886: if (ufs_dirbuf_dotdot_namlen(&dirbuf, vp) != 2 ||
887: dirbuf.dotdot_name[0] != '.' ||
888: dirbuf.dotdot_name[1] != '.')
889: /* XXX Panic? Print warning? */
890: return ENOTDIR;
891:
892: *ino_ret = ufs_rw32(dirbuf.dotdot_ino,
893: UFS_MPNEEDSWAP(VTOI(vp)->i_ump));
894: return 0;
895: }
896:
897: /*
898: * ufs_dirbuf_dotdot_namlen: Return the namlen of the directory buffer
899: * dirbuf that came from the directory vp. Swap byte order if
900: * necessary.
901: */
902: static int /* XXX int? uint8_t? */
903: ufs_dirbuf_dotdot_namlen(const struct dirtemplate *dirbuf,
904: const struct vnode *vp)
905: {
906: bool swap;
907:
908: KASSERT(dirbuf != NULL);
909: KASSERT(vp != NULL);
910: KASSERT(VTOI(vp) != NULL);
911: KASSERT(VTOI(vp)->i_ump != NULL);
912:
913: #if (BYTE_ORDER == LITTLE_ENDIAN)
914: swap = (UFS_MPNEEDSWAP(VTOI(vp)->i_ump) == 0);
915: #else
916: swap = (UFS_MPNEEDSWAP(VTOI(vp)->i_ump) != 0);
917: #endif
918:
919: return ((FSFMT(vp) && swap)?
920: dirbuf->dotdot_type : dirbuf->dotdot_namlen);
921: }
922:
923: /*
924: * ufs_gro_genealogy: Analyze the genealogy of the source and target
925: * directories.
926: */
927: int
928: ufs_gro_genealogy(struct mount *mp, kauth_cred_t cred,
929: struct vnode *fdvp, struct vnode *tdvp,
930: struct vnode **intermediate_node_ret)
931: {
932: struct vnode *vp, *dvp;
1.9 ! christos 933: ino_t dotdot_ino = 0; /* XXX: gcc */
1.1 riastrad 934: int error;
935:
936: KASSERT(mp != NULL);
937: KASSERT(fdvp != NULL);
938: KASSERT(tdvp != NULL);
939: KASSERT(fdvp != tdvp);
940: KASSERT(intermediate_node_ret != NULL);
941: KASSERT(fdvp->v_mount == mp);
942: KASSERT(tdvp->v_mount == mp);
943: KASSERT(fdvp->v_type == VDIR);
944: KASSERT(tdvp->v_type == VDIR);
945:
946: /*
947: * We need to provisionally lock tdvp to keep rmdir from
948: * deleting it -- or any ancestor -- at an inopportune moment.
949: */
950: error = ufs_gro_lock_directory(mp, tdvp);
951: if (error)
952: return error;
953:
954: vp = tdvp;
955: vref(vp);
956:
957: for (;;) {
958: KASSERT(vp != NULL);
959: KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
960: KASSERT(vp->v_mount == mp);
961: KASSERT(vp->v_type == VDIR);
962: KASSERT(!ufs_rmdired_p(vp));
963:
964: /* Did we hit the root without finding fdvp? */
1.6 dholland 965: if (VTOI(vp)->i_number == UFS_ROOTINO) {
1.1 riastrad 966: vput(vp);
967: *intermediate_node_ret = NULL;
968: return 0;
969: }
970:
971: error = ufs_read_dotdot(vp, cred, &dotdot_ino);
972: if (error) {
973: vput(vp);
974: return error;
975: }
976:
977: /* Did we find that fdvp is an ancestor of tdvp? */
978: if (VTOI(fdvp)->i_number == dotdot_ino) {
979: /* Unlock vp, but keep it referenced. */
980: VOP_UNLOCK(vp);
981: *intermediate_node_ret = vp;
982: return 0;
983: }
984:
985: /* Neither -- keep ascending the family tree. */
986:
987: /*
988: * Unlock vp so that we can lock the parent, but keep
989: * vp referenced until after we have found the parent,
990: * so that dotdot_ino will not be recycled.
991: *
992: * XXX This guarantees that vp's inode number will not
993: * be recycled, but why can't dotdot_ino be recycled?
994: */
995: VOP_UNLOCK(vp);
996: error = VFS_VGET(mp, dotdot_ino, &dvp);
997: vrele(vp);
998: if (error)
999: return error;
1000:
1001: KASSERT(dvp != NULL);
1002: KASSERT(VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
1003: vp = dvp;
1004:
1005: if (vp->v_type != VDIR) {
1006: /*
1007: * XXX Panic? Print a warning? Can this
1008: * happen if we lose the race I suspect to
1009: * exist above, and the `..' inode number has
1010: * been recycled?
1011: */
1012: vput(vp);
1013: return ENOTDIR;
1014: }
1015:
1016: if (ufs_rmdired_p(vp)) {
1017: vput(vp);
1018: return ENOENT;
1019: }
1020: }
1021: }
1022:
1023: /*
1024: * ufs_gro_lock_directory: Lock the directory vp, but fail if it has
1025: * been rmdir'd.
1026: */
1027: int
1028: ufs_gro_lock_directory(struct mount *mp, struct vnode *vp)
1029: {
1030:
1031: (void)mp;
1032: KASSERT(mp != NULL);
1033: KASSERT(vp != NULL);
1034: KASSERT(vp->v_mount == mp);
1035:
1036: vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1037:
1038: if (ufs_rmdired_p(vp)) {
1039: VOP_UNLOCK(vp);
1040: return ENOENT;
1041: }
1042:
1043: return 0;
1044: }
1045:
1046: static const struct genfs_rename_ops ufs_genfs_rename_ops = {
1047: .gro_directory_empty_p = ufs_gro_directory_empty_p,
1048: .gro_rename_check_possible = ufs_gro_rename_check_possible,
1049: .gro_rename_check_permitted = ufs_gro_rename_check_permitted,
1050: .gro_remove_check_possible = ufs_gro_remove_check_possible,
1051: .gro_remove_check_permitted = ufs_gro_remove_check_permitted,
1052: .gro_rename = ufs_gro_rename,
1053: .gro_remove = ufs_gro_remove,
1054: .gro_lookup = ufs_gro_lookup,
1055: .gro_genealogy = ufs_gro_genealogy,
1056: .gro_lock_directory = ufs_gro_lock_directory,
1057: };
CVSweb <webmaster@jp.NetBSD.org>