xref: /freebsd/sys/ufs/ufs/ufs_lookup.c (revision 5d3e7166)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)ufs_lookup.c	8.15 (Berkeley) 6/16/95
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include "opt_ufs.h"
43 #include "opt_quota.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/namei.h>
49 #include <sys/bio.h>
50 #include <sys/buf.h>
51 #include <sys/proc.h>
52 #include <sys/stat.h>
53 #include <sys/mount.h>
54 #include <sys/vnode.h>
55 #include <sys/sysctl.h>
56 
57 #include <vm/vm.h>
58 #include <vm/vm_extern.h>
59 
60 #include <ufs/ufs/extattr.h>
61 #include <ufs/ufs/quota.h>
62 #include <ufs/ufs/inode.h>
63 #include <ufs/ufs/dir.h>
64 #ifdef UFS_DIRHASH
65 #include <ufs/ufs/dirhash.h>
66 #endif
67 #include <ufs/ufs/ufsmount.h>
68 #include <ufs/ufs/ufs_extern.h>
69 #include <ufs/ffs/ffs_extern.h>
70 
71 #ifdef DIAGNOSTIC
72 static int	dirchk = 1;
73 #else
74 static int	dirchk = 0;
75 #endif
76 
77 SYSCTL_INT(_debug, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
78 
79 static int
80 ufs_delete_denied(struct vnode *vdp, struct vnode *tdp, struct ucred *cred,
81     struct thread *td)
82 {
83 	int error;
84 
85 #ifdef UFS_ACL
86 	/*
87 	 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
88 	 *
89 	 * 3.16.2.1. ACE4_DELETE vs. ACE4_DELETE_CHILD
90 	 */
91 
92 	/*
93 	 * XXX: Is this check required?
94 	 */
95 	error = VOP_ACCESS(vdp, VEXEC, cred, td);
96 	if (error)
97 		return (error);
98 
99 	error = VOP_ACCESSX(tdp, VDELETE, cred, td);
100 	if (error == 0)
101 		return (0);
102 
103 	error = VOP_ACCESSX(vdp, VDELETE_CHILD, cred, td);
104 	if (error == 0)
105 		return (0);
106 
107 	error = VOP_ACCESSX(vdp, VEXPLICIT_DENY | VDELETE_CHILD, cred, td);
108 	if (error)
109 		return (error);
110 
111 #endif /* !UFS_ACL */
112 
113 	/*
114 	 * Standard Unix access control - delete access requires VWRITE.
115 	 */
116 	error = VOP_ACCESS(vdp, VWRITE, cred, td);
117 	if (error)
118 		return (error);
119 
120 	/*
121 	 * If directory is "sticky", then user must own
122 	 * the directory, or the file in it, else she
123 	 * may not delete it (unless she's root). This
124 	 * implements append-only directories.
125 	 */
126 	if ((VTOI(vdp)->i_mode & ISVTX) &&
127 	    VOP_ACCESS(vdp, VADMIN, cred, td) &&
128 	    VOP_ACCESS(tdp, VADMIN, cred, td))
129 		return (EPERM);
130 
131 	return (0);
132 }
133 
134 /*
135  * Convert a component of a pathname into a pointer to a locked inode.
136  * This is a very central and rather complicated routine.
137  * If the filesystem is not maintained in a strict tree hierarchy,
138  * this can result in a deadlock situation (see comments in code below).
139  *
140  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
141  * on whether the name is to be looked up, created, renamed, or deleted.
142  * When CREATE, RENAME, or DELETE is specified, information usable in
143  * creating, renaming, or deleting a directory entry may be calculated.
144  * If flag has LOCKPARENT or'ed into it and the target of the pathname
145  * exists, lookup returns both the target and its parent directory locked.
146  * When creating or renaming and LOCKPARENT is specified, the target may
147  * not be ".".  When deleting and LOCKPARENT is specified, the target may
148  * be "."., but the caller must check to ensure it does an vrele and vput
149  * instead of two vputs.
150  *
151  * This routine is actually used as VOP_CACHEDLOOKUP method, and the
152  * filesystem employs the generic vfs_cache_lookup() as VOP_LOOKUP
153  * method.
154  *
155  * vfs_cache_lookup() performs the following for us:
156  *	check that it is a directory
157  *	check accessibility of directory
158  *	check for modification attempts on read-only mounts
159  *	if name found in cache
160  *	    if at end of path and deleting or creating
161  *		drop it
162  *	     else
163  *		return name.
164  *	return VOP_CACHEDLOOKUP()
165  *
166  * Overall outline of ufs_lookup:
167  *
168  *	search for name in directory, to found or notfound
169  * notfound:
170  *	if creating, return locked directory, leaving info on available slots
171  *	else return error
172  * found:
173  *	if at end of path and deleting, return information to allow delete
174  *	if at end of path and rewriting (RENAME and LOCKPARENT), lock target
175  *	  inode and return info to allow rewrite
176  *	if not at end, add name to cache; if at end and neither creating
177  *	  nor deleting, add name to cache
178  */
179 int
180 ufs_lookup(
181 	struct vop_cachedlookup_args /* {
182 		struct vnode *a_dvp;
183 		struct vnode **a_vpp;
184 		struct componentname *a_cnp;
185 	} */ *ap)
186 {
187 
188 	return (ufs_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
189 }
190 
191 int
192 ufs_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
193     ino_t *dd_ino)
194 {
195 	struct inode *dp;		/* inode for directory being searched */
196 	struct buf *bp;			/* a buffer of directory entries */
197 	struct direct *ep;		/* the current directory entry */
198 	int entryoffsetinblock;		/* offset of ep in bp's buffer */
199 	enum {NONE, COMPACT, FOUND} slotstatus;
200 	doff_t slotoffset;		/* offset of area with free space */
201 	doff_t i_diroff;		/* cached i_diroff value. */
202 	doff_t i_offset;		/* cached i_offset value. */
203 	int slotsize;			/* size of area at slotoffset */
204 	int slotfreespace;		/* amount of space free in slot */
205 	int slotneeded;			/* size of the entry we're seeking */
206 	int numdirpasses;		/* strategy for directory search */
207 	doff_t endsearch;		/* offset to end directory search */
208 	doff_t prevoff;			/* prev entry dp->i_offset */
209 	struct vnode *pdp;		/* saved dp during symlink work */
210 	struct vnode *tdp;		/* returned by VFS_VGET */
211 	doff_t enduseful;		/* pointer past last used dir slot */
212 	u_long bmask;			/* block offset mask */
213 	int namlen, error;
214 	struct ucred *cred = cnp->cn_cred;
215 	int flags = cnp->cn_flags;
216 	int nameiop = cnp->cn_nameiop;
217 	ino_t ino, ino1;
218 	int ltype;
219 
220 	if (vpp != NULL)
221 		*vpp = NULL;
222 
223 	dp = VTOI(vdp);
224 	if (dp->i_effnlink == 0)
225 		return (ENOENT);
226 
227 	/*
228 	 * Create a vm object if vmiodirenable is enabled.
229 	 * Alternatively we could call vnode_create_vobject
230 	 * in VFS_VGET but we could end up creating objects
231 	 * that are never used.
232 	 */
233 	vnode_create_vobject(vdp, DIP(dp, i_size), curthread);
234 
235 	bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
236 
237 #ifdef DEBUG_VFS_LOCKS
238 	/*
239 	 * Assert that the directory vnode is locked, and locked
240 	 * exclusively for the last component lookup for modifying
241 	 * operations.
242 	 *
243 	 * The directory-modifying operations need to save
244 	 * intermediate state in the inode between namei() call and
245 	 * actual directory manipulations.  See fields in the struct
246 	 * inode marked as 'used during directory lookup'.  We must
247 	 * ensure that upgrade in namei() does not happen, since
248 	 * upgrade might need to unlock vdp.  If quotas are enabled,
249 	 * getinoquota() also requires exclusive lock to modify inode.
250 	 */
251 	ASSERT_VOP_LOCKED(vdp, "ufs_lookup1");
252 	if ((nameiop == CREATE || nameiop == DELETE || nameiop == RENAME) &&
253 	    (flags & (LOCKPARENT | ISLASTCN)) == (LOCKPARENT | ISLASTCN))
254 		ASSERT_VOP_ELOCKED(vdp, "ufs_lookup2");
255 #endif
256 
257 restart:
258 	bp = NULL;
259 	slotoffset = -1;
260 
261 	/*
262 	 * We now have a segment name to search for, and a directory to search.
263 	 *
264 	 * Suppress search for slots unless creating
265 	 * file and at end of pathname, in which case
266 	 * we watch for a place to put the new file in
267 	 * case it doesn't already exist.
268 	 */
269 	ino = 0;
270 	i_diroff = dp->i_diroff;
271 	slotstatus = FOUND;
272 	slotfreespace = slotsize = slotneeded = 0;
273 	if ((nameiop == CREATE || nameiop == RENAME) &&
274 	    (flags & ISLASTCN)) {
275 		slotstatus = NONE;
276 		slotneeded = DIRECTSIZ(cnp->cn_namelen);
277 	}
278 
279 #ifdef UFS_DIRHASH
280 	/*
281 	 * Use dirhash for fast operations on large directories. The logic
282 	 * to determine whether to hash the directory is contained within
283 	 * ufsdirhash_build(); a zero return means that it decided to hash
284 	 * this directory and it successfully built up the hash table.
285 	 */
286 	if (ufsdirhash_build(dp) == 0) {
287 		/* Look for a free slot if needed. */
288 		enduseful = dp->i_size;
289 		if (slotstatus != FOUND) {
290 			slotoffset = ufsdirhash_findfree(dp, slotneeded,
291 			    &slotsize);
292 			if (slotoffset >= 0) {
293 				slotstatus = COMPACT;
294 				enduseful = ufsdirhash_enduseful(dp);
295 				if (enduseful < 0)
296 					enduseful = dp->i_size;
297 			}
298 		}
299 		/* Look up the component. */
300 		numdirpasses = 1;
301 		entryoffsetinblock = 0; /* silence compiler warning */
302 		switch (ufsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
303 		    &i_offset, &bp, nameiop == DELETE ? &prevoff : NULL)) {
304 		case 0:
305 			ep = (struct direct *)((char *)bp->b_data +
306 			    (i_offset & bmask));
307 			goto foundentry;
308 		case ENOENT:
309 			i_offset = roundup2(dp->i_size, DIRBLKSIZ);
310 			goto notfound;
311 		default:
312 			/* Something failed; just do a linear search. */
313 			break;
314 		}
315 	}
316 #endif /* UFS_DIRHASH */
317 	/*
318 	 * If there is cached information on a previous search of
319 	 * this directory, pick up where we last left off.
320 	 * We cache only lookups as these are the most common
321 	 * and have the greatest payoff. Caching CREATE has little
322 	 * benefit as it usually must search the entire directory
323 	 * to determine that the entry does not exist. Caching the
324 	 * location of the last DELETE or RENAME has not reduced
325 	 * profiling time and hence has been removed in the interest
326 	 * of simplicity.
327 	 */
328 	if (nameiop != LOOKUP || i_diroff == 0 || i_diroff >= dp->i_size) {
329 		entryoffsetinblock = 0;
330 		i_offset = 0;
331 		numdirpasses = 1;
332 	} else {
333 		i_offset = i_diroff;
334 		if ((entryoffsetinblock = i_offset & bmask) &&
335 		    (error = UFS_BLKATOFF(vdp, (off_t)i_offset, NULL, &bp)))
336 			return (error);
337 		numdirpasses = 2;
338 		nchstats.ncs_2passes++;
339 	}
340 	prevoff = i_offset;
341 	endsearch = roundup2(dp->i_size, DIRBLKSIZ);
342 	enduseful = 0;
343 
344 searchloop:
345 	while (i_offset < endsearch) {
346 		/*
347 		 * If necessary, get the next directory block.
348 		 */
349 		if ((i_offset & bmask) == 0) {
350 			if (bp != NULL)
351 				brelse(bp);
352 			error =
353 			    UFS_BLKATOFF(vdp, (off_t)i_offset, NULL, &bp);
354 			if (error)
355 				return (error);
356 			entryoffsetinblock = 0;
357 		}
358 		/*
359 		 * If still looking for a slot, and at a DIRBLKSIZE
360 		 * boundary, have to start looking for free space again.
361 		 */
362 		if (slotstatus == NONE &&
363 		    (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
364 			slotoffset = -1;
365 			slotfreespace = 0;
366 		}
367 		/*
368 		 * Get pointer to next entry.
369 		 * Full validation checks are slow, so we only check
370 		 * enough to insure forward progress through the
371 		 * directory. Complete checks can be run by patching
372 		 * "dirchk" to be true.
373 		 */
374 		ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
375 		if (ep->d_reclen == 0 || ep->d_reclen >
376 		    DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
377 		    (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) {
378 			int i;
379 
380 			ufs_dirbad(dp, i_offset, "mangled entry");
381 			i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
382 			i_offset += i;
383 			entryoffsetinblock += i;
384 			continue;
385 		}
386 
387 		/*
388 		 * If an appropriate sized slot has not yet been found,
389 		 * check to see if one is available. Also accumulate space
390 		 * in the current block so that we can determine if
391 		 * compaction is viable.
392 		 */
393 		if (slotstatus != FOUND) {
394 			int size = ep->d_reclen;
395 
396 			if (ep->d_ino != 0)
397 				size -= DIRSIZ(OFSFMT(vdp), ep);
398 			if (size > 0) {
399 				if (size >= slotneeded) {
400 					slotstatus = FOUND;
401 					slotoffset = i_offset;
402 					slotsize = ep->d_reclen;
403 				} else if (slotstatus == NONE) {
404 					slotfreespace += size;
405 					if (slotoffset == -1)
406 						slotoffset = i_offset;
407 					if (slotfreespace >= slotneeded) {
408 						slotstatus = COMPACT;
409 						slotsize = i_offset +
410 						      ep->d_reclen - slotoffset;
411 					}
412 				}
413 			}
414 		}
415 
416 		/*
417 		 * Check for a name match.
418 		 */
419 		if (ep->d_ino) {
420 #			if (BYTE_ORDER == LITTLE_ENDIAN)
421 				if (OFSFMT(vdp))
422 					namlen = ep->d_type;
423 				else
424 					namlen = ep->d_namlen;
425 #			else
426 				namlen = ep->d_namlen;
427 #			endif
428 			if (namlen == cnp->cn_namelen &&
429 				(cnp->cn_nameptr[0] == ep->d_name[0]) &&
430 			    !bcmp(cnp->cn_nameptr, ep->d_name,
431 				(unsigned)namlen)) {
432 #ifdef UFS_DIRHASH
433 foundentry:
434 #endif
435 				/*
436 				 * Save directory entry's inode number and
437 				 * reclen in ndp->ni_ufs area, and release
438 				 * directory buffer.
439 				 */
440 				if (!OFSFMT(vdp) && ep->d_type == DT_WHT) {
441 					slotstatus = FOUND;
442 					slotoffset = i_offset;
443 					slotsize = ep->d_reclen;
444 					enduseful = dp->i_size;
445 					cnp->cn_flags |= ISWHITEOUT;
446 					numdirpasses--;
447 					goto notfound;
448 				}
449 				ino = ep->d_ino;
450 				goto found;
451 			}
452 		}
453 		prevoff = i_offset;
454 		i_offset += ep->d_reclen;
455 		entryoffsetinblock += ep->d_reclen;
456 		if (ep->d_ino)
457 			enduseful = i_offset;
458 	}
459 notfound:
460 	/*
461 	 * If we started in the middle of the directory and failed
462 	 * to find our target, we must check the beginning as well.
463 	 */
464 	if (numdirpasses == 2) {
465 		numdirpasses--;
466 		i_offset = 0;
467 		endsearch = i_diroff;
468 		goto searchloop;
469 	}
470 	if (bp != NULL)
471 		brelse(bp);
472 	/*
473 	 * If creating, and at end of pathname and current
474 	 * directory has not been removed, then can consider
475 	 * allowing file to be created.
476 	 */
477 	if ((nameiop == CREATE || nameiop == RENAME ||
478 	     (nameiop == DELETE &&
479 	      (cnp->cn_flags & DOWHITEOUT) &&
480 	      (cnp->cn_flags & ISWHITEOUT))) &&
481 	    (flags & ISLASTCN) && dp->i_effnlink != 0) {
482 		/*
483 		 * Access for write is interpreted as allowing
484 		 * creation of files in the directory.
485 		 *
486 		 * XXX: Fix the comment above.
487 		 */
488 		if (flags & WILLBEDIR)
489 			error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, curthread);
490 		else
491 			error = VOP_ACCESS(vdp, VWRITE, cred, curthread);
492 		if (error)
493 			return (error);
494 		/*
495 		 * Return an indication of where the new directory
496 		 * entry should be put.  If we didn't find a slot,
497 		 * then set dp->i_count to 0 indicating
498 		 * that the new slot belongs at the end of the
499 		 * directory. If we found a slot, then the new entry
500 		 * can be put in the range from dp->i_offset to
501 		 * dp->i_offset + dp->i_count.
502 		 */
503 		if (slotstatus == NONE) {
504 			SET_I_OFFSET(dp, roundup2(dp->i_size, DIRBLKSIZ));
505 			SET_I_COUNT(dp, 0);
506 			enduseful = I_OFFSET(dp);
507 		} else if (nameiop == DELETE) {
508 			SET_I_OFFSET(dp, slotoffset);
509 			if ((I_OFFSET(dp) & (DIRBLKSIZ - 1)) == 0)
510 				SET_I_COUNT(dp, 0);
511 			else
512 				SET_I_COUNT(dp, I_OFFSET(dp) - prevoff);
513 		} else {
514 			SET_I_OFFSET(dp, slotoffset);
515 			SET_I_COUNT(dp, slotsize);
516 			if (enduseful < slotoffset + slotsize)
517 				enduseful = slotoffset + slotsize;
518 		}
519 		SET_I_ENDOFF(dp, roundup2(enduseful, DIRBLKSIZ));
520 		/*
521 		 * We return with the directory locked, so that
522 		 * the parameters we set up above will still be
523 		 * valid if we actually decide to do a direnter().
524 		 * We return ni_vp == NULL to indicate that the entry
525 		 * does not currently exist; we leave a pointer to
526 		 * the (locked) directory inode in ndp->ni_dvp.
527 		 *
528 		 * NB - if the directory is unlocked, then this
529 		 * information cannot be used.
530 		 */
531 		return (EJUSTRETURN);
532 	}
533 	/*
534 	 * Insert name into cache (as non-existent) if appropriate.
535 	 */
536 	if ((cnp->cn_flags & MAKEENTRY) != 0)
537 		cache_enter(vdp, NULL, cnp);
538 	return (ENOENT);
539 
540 found:
541 	if (dd_ino != NULL)
542 		*dd_ino = ino;
543 	if (numdirpasses == 2)
544 		nchstats.ncs_pass2++;
545 	/*
546 	 * Check that directory length properly reflects presence
547 	 * of this entry.
548 	 */
549 	if (i_offset + DIRSIZ(OFSFMT(vdp), ep) > dp->i_size) {
550 		ufs_dirbad(dp, i_offset, "i_size too small");
551 		brelse(bp);
552 		return (EIO);
553 	}
554 	brelse(bp);
555 
556 	/*
557 	 * Found component in pathname.
558 	 * If the final component of path name, save information
559 	 * in the cache as to where the entry was found.
560 	 */
561 	if ((flags & ISLASTCN) && nameiop == LOOKUP)
562 		dp->i_diroff = rounddown2(i_offset, DIRBLKSIZ);
563 
564 	/*
565 	 * If deleting, and at end of pathname, return
566 	 * parameters which can be used to remove file.
567 	 */
568 	if (nameiop == DELETE && (flags & ISLASTCN)) {
569 		if (flags & LOCKPARENT)
570 			ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
571 
572 		if (VOP_ISLOCKED(vdp) == LK_EXCLUSIVE) {
573 			/*
574 			 * Return pointer to current entry in
575 			 * dp->i_offset, and distance past previous
576 			 * entry (if there is a previous entry in this
577 			 * block) in dp->i_count.
578 			 *
579 			 * We shouldn't be setting these in the
580 			 * WANTPARENT case (first lookup in rename()), but any
581 			 * lookups that will result in directory changes will
582 			 * overwrite these.
583 			 */
584 			SET_I_OFFSET(dp, i_offset);
585 			if ((I_OFFSET(dp) & (DIRBLKSIZ - 1)) == 0)
586 				SET_I_COUNT(dp, 0);
587 			else
588 				SET_I_COUNT(dp, I_OFFSET(dp) - prevoff);
589 		}
590 		if (dd_ino != NULL)
591 			return (0);
592 
593 		/*
594 		 * Save directory inode pointer in ndp->ni_dvp for
595 		 * dirremove().
596 		 */
597 		if ((error = VFS_VGET(vdp->v_mount, ino,
598 		    LK_EXCLUSIVE, &tdp)) != 0)
599 			return (error);
600 		error = ufs_delete_denied(vdp, tdp, cred, curthread);
601 		if (error) {
602 			vput(tdp);
603 			return (error);
604 		}
605 		if (dp->i_number == ino) {
606 			VREF(vdp);
607 			*vpp = vdp;
608 			vput(tdp);
609 			return (0);
610 		}
611 
612 		*vpp = tdp;
613 		return (0);
614 	}
615 
616 	/*
617 	 * If rewriting (RENAME), return the inode and the
618 	 * information required to rewrite the present directory
619 	 * Must get inode of directory entry to verify it's a
620 	 * regular file, or empty directory.
621 	 */
622 	if (nameiop == RENAME && (flags & ISLASTCN)) {
623 		if (flags & WILLBEDIR)
624 			error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, curthread);
625 		else
626 			error = VOP_ACCESS(vdp, VWRITE, cred, curthread);
627 		if (error)
628 			return (error);
629 		/*
630 		 * Careful about locking second inode.
631 		 * This can only occur if the target is ".".
632 		 */
633 		SET_I_OFFSET(dp, i_offset);
634 		if (dp->i_number == ino)
635 			return (EISDIR);
636 		if (dd_ino != NULL)
637 			return (0);
638 		if ((error = VFS_VGET(vdp->v_mount, ino,
639 		    LK_EXCLUSIVE, &tdp)) != 0)
640 			return (error);
641 
642 		error = ufs_delete_denied(vdp, tdp, cred, curthread);
643 		if (error) {
644 			vput(tdp);
645 			return (error);
646 		}
647 
648 #ifdef SunOS_doesnt_do_that
649 		/*
650 		 * The only purpose of this check is to return the correct
651 		 * error.  Assume that we want to rename directory "a"
652 		 * to a file "b", and that we have no ACL_WRITE_DATA on
653 		 * a containing directory, but we _do_ have ACL_APPEND_DATA.
654 		 * In that case, the VOP_ACCESS check above will return 0,
655 		 * and the operation will fail with ENOTDIR instead
656 		 * of EACCESS.
657 		 */
658 		if (tdp->v_type == VDIR)
659 			error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, curthread);
660 		else
661 			error = VOP_ACCESS(vdp, VWRITE, cred, curthread);
662 		if (error) {
663 			vput(tdp);
664 			return (error);
665 		}
666 #endif
667 
668 		*vpp = tdp;
669 		return (0);
670 	}
671 	if (dd_ino != NULL)
672 		return (0);
673 
674 	/*
675 	 * Step through the translation in the name.  We do not `vput' the
676 	 * directory because we may need it again if a symbolic link
677 	 * is relative to the current directory.  Instead we save it
678 	 * unlocked as "pdp".  We must get the target inode before unlocking
679 	 * the directory to insure that the inode will not be removed
680 	 * before we get it.  We prevent deadlock by always fetching
681 	 * inodes from the root, moving down the directory tree. Thus
682 	 * when following backward pointers ".." we must unlock the
683 	 * parent directory before getting the requested directory.
684 	 * There is a potential race condition here if both the current
685 	 * and parent directories are removed before the VFS_VGET for the
686 	 * inode associated with ".." returns.  We hope that this occurs
687 	 * infrequently since we cannot avoid this race condition without
688 	 * implementing a sophisticated deadlock detection algorithm.
689 	 * Note also that this simple deadlock detection scheme will not
690 	 * work if the filesystem has any hard links other than ".."
691 	 * that point backwards in the directory structure.
692 	 */
693 	pdp = vdp;
694 	if (flags & ISDOTDOT) {
695 		error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
696 		if (error)
697 			return (error);
698 
699 		/*
700 		 * Recheck that ".." entry in the vdp directory points
701 		 * to the inode we looked up before vdp lock was
702 		 * dropped.
703 		 */
704 		error = ufs_lookup_ino(pdp, NULL, cnp, &ino1);
705 		if (error) {
706 			vput(tdp);
707 			return (error);
708 		}
709 		if (ino1 != ino) {
710 			vput(tdp);
711 			goto restart;
712 		}
713 
714 		*vpp = tdp;
715 	} else if (dp->i_number == ino) {
716 		VREF(vdp);	/* we want ourself, ie "." */
717 		/*
718 		 * When we lookup "." we still can be asked to lock it
719 		 * differently.
720 		 */
721 		ltype = cnp->cn_lkflags & LK_TYPE_MASK;
722 		if (ltype != VOP_ISLOCKED(vdp)) {
723 			if (ltype == LK_EXCLUSIVE)
724 				vn_lock(vdp, LK_UPGRADE | LK_RETRY);
725 			else /* if (ltype == LK_SHARED) */
726 				vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
727 			/*
728 			 * Relock for the "." case may left us with
729 			 * reclaimed vnode.
730 			 */
731 			if (VN_IS_DOOMED(vdp)) {
732 				vrele(vdp);
733 				return (ENOENT);
734 			}
735 		}
736 		*vpp = vdp;
737 	} else {
738 		error = VFS_VGET(pdp->v_mount, ino, cnp->cn_lkflags, &tdp);
739 		if (error == 0 && VTOI(tdp)->i_mode == 0) {
740 			vgone(tdp);
741 			vput(tdp);
742 			error = ENOENT;
743 		}
744 		if (error)
745 			return (error);
746 		*vpp = tdp;
747 	}
748 
749 	/*
750 	 * Insert name into cache if appropriate.
751 	 */
752 	if (cnp->cn_flags & MAKEENTRY)
753 		cache_enter(vdp, *vpp, cnp);
754 	return (0);
755 }
756 
757 void
758 ufs_dirbad(struct inode *ip, doff_t offset, char *how)
759 {
760 
761 	(void)printf("%s: bad dir ino %ju at offset %ld: %s\n",
762 	    ITOV(ip)->v_mount->mnt_stat.f_mntonname, (uintmax_t)ip->i_number,
763 	    (long)offset, how);
764 }
765 
766 /*
767  * Do consistency checking on a directory entry:
768  *	record length must be multiple of 4
769  *	entry must fit in rest of its DIRBLKSIZ block
770  *	record must be large enough to contain entry
771  *	name is not longer than UFS_MAXNAMLEN
772  *	name must be as long as advertised, and null terminated
773  */
774 int
775 ufs_dirbadentry(struct vnode *dp, struct direct *ep, int entryoffsetinblock)
776 {
777 	int i, namlen;
778 
779 #	if (BYTE_ORDER == LITTLE_ENDIAN)
780 		if (OFSFMT(dp))
781 			namlen = ep->d_type;
782 		else
783 			namlen = ep->d_namlen;
784 #	else
785 		namlen = ep->d_namlen;
786 #	endif
787 	if ((ep->d_reclen & 0x3) != 0 ||
788 	    ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
789 	    ep->d_reclen < DIRSIZ(OFSFMT(dp), ep) || namlen > UFS_MAXNAMLEN) {
790 		/*return (1); */
791 		printf("First bad\n");
792 		goto bad;
793 	}
794 	if (ep->d_ino == 0)
795 		return (0);
796 	for (i = 0; i < namlen; i++)
797 		if (ep->d_name[i] == '\0') {
798 			/*return (1); */
799 			printf("Second bad\n");
800 			goto bad;
801 		}
802 	if (ep->d_name[i])
803 		goto bad;
804 	return (0);
805 bad:
806 	return (1);
807 }
808 
809 /*
810  * Construct a new directory entry after a call to namei, using the
811  * parameters that it left in the componentname argument cnp. The
812  * argument ip is the inode to which the new directory entry will refer.
813  */
814 void
815 ufs_makedirentry(struct inode *ip, struct componentname *cnp,
816     struct direct *newdirp)
817 {
818 	u_int namelen;
819 
820 	namelen = (unsigned)cnp->cn_namelen;
821 	KASSERT(namelen <= UFS_MAXNAMLEN,
822 		("ufs_makedirentry: name too long"));
823 	newdirp->d_ino = ip->i_number;
824 	newdirp->d_namlen = namelen;
825 
826 	/* Zero out after-name padding */
827 	*(u_int32_t *)(&newdirp->d_name[namelen & ~(DIR_ROUNDUP - 1)]) = 0;
828 
829 	bcopy(cnp->cn_nameptr, newdirp->d_name, namelen);
830 
831 	if (!OFSFMT(ITOV(ip)))
832 		newdirp->d_type = IFTODT(ip->i_mode);
833 	else {
834 		newdirp->d_type = 0;
835 #		if (BYTE_ORDER == LITTLE_ENDIAN)
836 			{ u_char tmp = newdirp->d_namlen;
837 			newdirp->d_namlen = newdirp->d_type;
838 			newdirp->d_type = tmp; }
839 #		endif
840 	}
841 }
842 
843 /*
844  * Write a directory entry after a call to namei, using the parameters
845  * that it left in nameidata. The argument dirp is the new directory
846  * entry contents. Dvp is a pointer to the directory to be written,
847  * which was left locked by namei. Remaining parameters (dp->i_offset,
848  * dp->i_count) indicate how the space for the new entry is to be obtained.
849  * Non-null bp indicates that a directory is being created (for the
850  * soft dependency code).
851  */
852 int
853 ufs_direnter(struct vnode *dvp, struct vnode *tvp, struct direct *dirp,
854     struct componentname *cnp, struct buf *newdirbp)
855 {
856 	struct ucred *cr;
857 	struct thread *td;
858 	int newentrysize;
859 	struct inode *dp;
860 	struct buf *bp;
861 	u_int dsize;
862 	struct direct *ep, *nep;
863 	u_int64_t old_isize;
864 	int error, ret, blkoff, loc, spacefree, flags, namlen;
865 	char *dirbuf;
866 
867 	td = curthread;	/* XXX */
868 	cr = td->td_ucred;
869 
870 	dp = VTOI(dvp);
871 	newentrysize = DIRSIZ(OFSFMT(dvp), dirp);
872 
873 	if (I_COUNT(dp) == 0) {
874 		/*
875 		 * If dp->i_count is 0, then namei could find no
876 		 * space in the directory. Here, dp->i_offset will
877 		 * be on a directory block boundary and we will write the
878 		 * new entry into a fresh block.
879 		 */
880 		if (I_OFFSET(dp) & (DIRBLKSIZ - 1))
881 			panic("ufs_direnter: newblk");
882 		flags = BA_CLRBUF;
883 		if (!DOINGSOFTDEP(dvp) && !DOINGASYNC(dvp))
884 			flags |= IO_SYNC;
885 #ifdef QUOTA
886 		if ((error = getinoquota(dp)) != 0) {
887 			if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
888 				bdwrite(newdirbp);
889 			return (error);
890 		}
891 #endif
892 		old_isize = dp->i_size;
893 		vnode_pager_setsize(dvp, (u_long)I_OFFSET(dp) + DIRBLKSIZ);
894 		if ((error = UFS_BALLOC(dvp, (off_t)I_OFFSET(dp), DIRBLKSIZ,
895 		    cr, flags, &bp)) != 0) {
896 			if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
897 				bdwrite(newdirbp);
898 			vnode_pager_setsize(dvp, (u_long)old_isize);
899 			return (error);
900 		}
901 		dp->i_size = I_OFFSET(dp) + DIRBLKSIZ;
902 		DIP_SET(dp, i_size, dp->i_size);
903 		SET_I_ENDOFF(dp, dp->i_size);
904 		UFS_INODE_SET_FLAG(dp, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
905 		dirp->d_reclen = DIRBLKSIZ;
906 		blkoff = I_OFFSET(dp) &
907 		    (VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_iosize - 1);
908 		bcopy((caddr_t)dirp, (caddr_t)bp->b_data + blkoff,newentrysize);
909 #ifdef UFS_DIRHASH
910 		if (dp->i_dirhash != NULL) {
911 			ufsdirhash_newblk(dp, I_OFFSET(dp));
912 			ufsdirhash_add(dp, dirp, I_OFFSET(dp));
913 			ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff,
914 			    I_OFFSET(dp));
915 		}
916 #endif
917 		if (DOINGSOFTDEP(dvp)) {
918 			/*
919 			 * Ensure that the entire newly allocated block is a
920 			 * valid directory so that future growth within the
921 			 * block does not have to ensure that the block is
922 			 * written before the inode.
923 			 */
924 			blkoff += DIRBLKSIZ;
925 			while (blkoff < bp->b_bcount) {
926 				((struct direct *)
927 				   (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
928 				blkoff += DIRBLKSIZ;
929 			}
930 			if (softdep_setup_directory_add(bp, dp, I_OFFSET(dp),
931 			    dirp->d_ino, newdirbp, 1))
932 				UFS_INODE_SET_FLAG(dp, IN_NEEDSYNC);
933 			if (newdirbp)
934 				bdwrite(newdirbp);
935 			bdwrite(bp);
936 			return (UFS_UPDATE(dvp, 0));
937 		}
938 		if (DOINGASYNC(dvp)) {
939 			bdwrite(bp);
940 			return (UFS_UPDATE(dvp, 0));
941 		}
942 		error = bwrite(bp);
943 		ret = UFS_UPDATE(dvp, 1);
944 		if (error == 0)
945 			return (ret);
946 		return (error);
947 	}
948 
949 	/*
950 	 * If dp->i_count is non-zero, then namei found space for the new
951 	 * entry in the range dp->i_offset to dp->i_offset + dp->i_count
952 	 * in the directory. To use this space, we may have to compact
953 	 * the entries located there, by copying them together towards the
954 	 * beginning of the block, leaving the free space in one usable
955 	 * chunk at the end.
956 	 */
957 
958 	/*
959 	 * Increase size of directory if entry eats into new space.
960 	 * This should never push the size past a new multiple of
961 	 * DIRBLKSIZE.
962 	 *
963 	 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
964 	 */
965 	if (I_OFFSET(dp) + I_COUNT(dp) > dp->i_size) {
966 		dp->i_size = I_OFFSET(dp) + I_COUNT(dp);
967 		DIP_SET(dp, i_size, dp->i_size);
968 		UFS_INODE_SET_FLAG(dp, IN_SIZEMOD | IN_MODIFIED);
969 	}
970 	/*
971 	 * Get the block containing the space for the new directory entry.
972 	 */
973 	error = UFS_BLKATOFF(dvp, (off_t)I_OFFSET(dp), &dirbuf, &bp);
974 	if (error) {
975 		if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
976 			bdwrite(newdirbp);
977 		return (error);
978 	}
979 	/*
980 	 * Find space for the new entry. In the simple case, the entry at
981 	 * offset base will have the space. If it does not, then namei
982 	 * arranged that compacting the region dp->i_offset to
983 	 * dp->i_offset + dp->i_count would yield the space.
984 	 */
985 	ep = (struct direct *)dirbuf;
986 	dsize = ep->d_ino ? DIRSIZ(OFSFMT(dvp), ep) : 0;
987 	spacefree = ep->d_reclen - dsize;
988 	for (loc = ep->d_reclen; loc < I_COUNT(dp); ) {
989 		nep = (struct direct *)(dirbuf + loc);
990 
991 		/* Trim the existing slot (NB: dsize may be zero). */
992 		ep->d_reclen = dsize;
993 		ep = (struct direct *)((char *)ep + dsize);
994 
995 		/* Read nep->d_reclen now as the bcopy() may clobber it. */
996 		loc += nep->d_reclen;
997 		if (nep->d_ino == 0) {
998 			/*
999 			 * A mid-block unused entry. Such entries are
1000 			 * never created by the kernel, but fsck_ffs
1001 			 * can create them (and it doesn't fix them).
1002 			 *
1003 			 * Add up the free space, and initialise the
1004 			 * relocated entry since we don't bcopy it.
1005 			 */
1006 			spacefree += nep->d_reclen;
1007 			ep->d_ino = 0;
1008 			dsize = 0;
1009 			continue;
1010 		}
1011 		dsize = DIRSIZ(OFSFMT(dvp), nep);
1012 		spacefree += nep->d_reclen - dsize;
1013 #ifdef UFS_DIRHASH
1014 		if (dp->i_dirhash != NULL)
1015 			ufsdirhash_move(dp, nep,
1016 			    I_OFFSET(dp) + ((char *)nep - dirbuf),
1017 			    I_OFFSET(dp) + ((char *)ep - dirbuf));
1018 #endif
1019 		if (DOINGSOFTDEP(dvp))
1020 			softdep_change_directoryentry_offset(bp, dp, dirbuf,
1021 			    (caddr_t)nep, (caddr_t)ep, dsize);
1022 		else
1023 			bcopy((caddr_t)nep, (caddr_t)ep, dsize);
1024 	}
1025 	/*
1026 	 * Here, `ep' points to a directory entry containing `dsize' in-use
1027 	 * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
1028 	 * then the entry is completely unused (dsize == 0). The value
1029 	 * of ep->d_reclen is always indeterminate.
1030 	 *
1031 	 * Update the pointer fields in the previous entry (if any),
1032 	 * copy in the new entry, and write out the block.
1033 	 */
1034 #	if (BYTE_ORDER == LITTLE_ENDIAN)
1035 		if (OFSFMT(dvp))
1036 			namlen = ep->d_type;
1037 		else
1038 			namlen = ep->d_namlen;
1039 #	else
1040 		namlen = ep->d_namlen;
1041 #	endif
1042 	if (ep->d_ino == 0 ||
1043 	    (ep->d_ino == UFS_WINO && namlen == dirp->d_namlen &&
1044 	     bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
1045 		if (spacefree + dsize < newentrysize)
1046 			panic("ufs_direnter: compact1");
1047 		dirp->d_reclen = spacefree + dsize;
1048 	} else {
1049 		if (spacefree < newentrysize)
1050 			panic("ufs_direnter: compact2");
1051 		dirp->d_reclen = spacefree;
1052 		ep->d_reclen = dsize;
1053 		ep = (struct direct *)((char *)ep + dsize);
1054 	}
1055 #ifdef UFS_DIRHASH
1056 	if (dp->i_dirhash != NULL && (ep->d_ino == 0 ||
1057 	    dirp->d_reclen == spacefree))
1058 		ufsdirhash_add(dp, dirp, I_OFFSET(dp) + ((char *)ep - dirbuf));
1059 #endif
1060 	bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize);
1061 #ifdef UFS_DIRHASH
1062 	if (dp->i_dirhash != NULL)
1063 		ufsdirhash_checkblock(dp, dirbuf -
1064 		    (I_OFFSET(dp) & (DIRBLKSIZ - 1)),
1065 		    rounddown2(I_OFFSET(dp), DIRBLKSIZ));
1066 #endif
1067 
1068 	if (DOINGSOFTDEP(dvp)) {
1069 		(void) softdep_setup_directory_add(bp, dp,
1070 		    I_OFFSET(dp) + (caddr_t)ep - dirbuf,
1071 		    dirp->d_ino, newdirbp, 0);
1072 		if (newdirbp != NULL)
1073 			bdwrite(newdirbp);
1074 		bdwrite(bp);
1075 	} else {
1076 		if (DOINGASYNC(dvp)) {
1077 			bdwrite(bp);
1078 			error = 0;
1079 		} else {
1080 			error = bwrite(bp);
1081 		}
1082 	}
1083 
1084 	/*
1085 	 * If all went well, and the directory can be shortened,
1086 	 * mark directory inode with the truncation request.
1087 	 */
1088 	UFS_INODE_SET_FLAG(dp, IN_CHANGE | IN_UPDATE | (error == 0 &&
1089 	    I_ENDOFF(dp) != 0 && I_ENDOFF(dp) < dp->i_size ? IN_ENDOFF : 0));
1090 
1091 	return (error);
1092 }
1093 
1094 /*
1095  * Remove a directory entry after a call to namei, using
1096  * the parameters which it left in nameidata. The entry
1097  * dp->i_offset contains the offset into the directory of the
1098  * entry to be eliminated.  The dp->i_count field contains the
1099  * size of the previous record in the directory.  If this
1100  * is 0, the first entry is being deleted, so we need only
1101  * zero the inode number to mark the entry as free.  If the
1102  * entry is not the first in the directory, we must reclaim
1103  * the space of the now empty record by adding the record size
1104  * to the size of the previous entry.
1105  */
1106 int
1107 ufs_dirremove(struct vnode *dvp, struct inode *ip, int flags, int isrmdir)
1108 {
1109 	struct inode *dp;
1110 	struct direct *ep, *rep;
1111 	struct buf *bp;
1112 	off_t offset;
1113 	int error;
1114 
1115 	dp = VTOI(dvp);
1116 
1117 	/*
1118 	 * Adjust the link count early so softdep can block if necessary.
1119 	 */
1120 	if (ip) {
1121 		ip->i_effnlink--;
1122 		UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1123 		if (DOINGSOFTDEP(dvp)) {
1124 			softdep_setup_unlink(dp, ip);
1125 		} else {
1126 			ip->i_nlink--;
1127 			DIP_SET(ip, i_nlink, ip->i_nlink);
1128 			UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1129 		}
1130 	}
1131 	if (flags & DOWHITEOUT)
1132 		offset = I_OFFSET(dp);
1133 	else
1134 		offset = I_OFFSET(dp) - I_COUNT(dp);
1135 	if ((error = UFS_BLKATOFF(dvp, offset, (char **)&ep, &bp)) != 0) {
1136 		if (ip) {
1137 			ip->i_effnlink++;
1138 			UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1139 			if (DOINGSOFTDEP(dvp)) {
1140 				softdep_change_linkcnt(ip);
1141 			} else {
1142 				ip->i_nlink++;
1143 				DIP_SET(ip, i_nlink, ip->i_nlink);
1144 				UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1145 			}
1146 		}
1147 		return (error);
1148 	}
1149 	if (flags & DOWHITEOUT) {
1150 		/*
1151 		 * Whiteout entry: set d_ino to UFS_WINO.
1152 		 */
1153 		ep->d_ino = UFS_WINO;
1154 		ep->d_type = DT_WHT;
1155 		goto out;
1156 	}
1157 	/* Set 'rep' to the entry being removed. */
1158 	if (I_COUNT(dp) == 0)
1159 		rep = ep;
1160 	else
1161 		rep = (struct direct *)((char *)ep + ep->d_reclen);
1162 #ifdef UFS_DIRHASH
1163 	/*
1164 	 * Remove the dirhash entry. This is complicated by the fact
1165 	 * that `ep' is the previous entry when dp->i_count != 0.
1166 	 */
1167 	if (dp->i_dirhash != NULL)
1168 		ufsdirhash_remove(dp, rep, I_OFFSET(dp));
1169 #endif
1170 	if (ip && rep->d_ino != ip->i_number)
1171 		panic("ufs_dirremove: ip %ju does not match dirent ino %ju\n",
1172 		    (uintmax_t)ip->i_number, (uintmax_t)rep->d_ino);
1173 	/*
1174 	 * Zero out the file directory entry metadata to reduce disk
1175 	 * scavenging disclosure.
1176 	 */
1177 	bzero(&rep->d_name[0], rep->d_namlen);
1178 	rep->d_namlen = 0;
1179 	rep->d_type = 0;
1180 	rep->d_ino = 0;
1181 
1182 	if (I_COUNT(dp) != 0) {
1183 		/*
1184 		 * Collapse new free space into previous entry.
1185 		 */
1186 		ep->d_reclen += rep->d_reclen;
1187 		rep->d_reclen = 0;
1188 	}
1189 #ifdef UFS_DIRHASH
1190 	if (dp->i_dirhash != NULL)
1191 		ufsdirhash_checkblock(dp, (char *)ep -
1192 		    ((I_OFFSET(dp) - I_COUNT(dp)) & (DIRBLKSIZ - 1)),
1193 		    rounddown2(I_OFFSET(dp), DIRBLKSIZ));
1194 #endif
1195 out:
1196 	error = 0;
1197 	if (DOINGSOFTDEP(dvp)) {
1198 		if (ip)
1199 			softdep_setup_remove(bp, dp, ip, isrmdir);
1200 		if (softdep_slowdown(dvp))
1201 			error = bwrite(bp);
1202 		else
1203 			bdwrite(bp);
1204 	} else {
1205 		if (flags & DOWHITEOUT)
1206 			error = bwrite(bp);
1207 		else if (DOINGASYNC(dvp))
1208 			bdwrite(bp);
1209 		else
1210 			error = bwrite(bp);
1211 	}
1212 	UFS_INODE_SET_FLAG(dp, IN_CHANGE | IN_UPDATE);
1213 	/*
1214 	 * If the last named reference to a snapshot goes away,
1215 	 * drop its snapshot reference so that it will be reclaimed
1216 	 * when last open reference goes away.
1217 	 */
1218 	if (ip != NULL && IS_SNAPSHOT(ip) && ip->i_effnlink == 0)
1219 		UFS_SNAPGONE(ip);
1220 	return (error);
1221 }
1222 
1223 /*
1224  * Rewrite an existing directory entry to point at the inode
1225  * supplied.  The parameters describing the directory entry are
1226  * set up by a call to namei.
1227  */
1228 int
1229 ufs_dirrewrite(struct inode *dp, struct inode *oip, ino_t newinum, int newtype,
1230     int isrmdir)
1231 {
1232 	struct buf *bp;
1233 	struct direct *ep;
1234 	struct vnode *vdp = ITOV(dp);
1235 	int error;
1236 
1237 	/*
1238 	 * Drop the link before we lock the buf so softdep can block if
1239 	 * necessary.
1240 	 */
1241 	oip->i_effnlink--;
1242 	UFS_INODE_SET_FLAG(oip, IN_CHANGE);
1243 	if (DOINGSOFTDEP(vdp)) {
1244 		softdep_setup_unlink(dp, oip);
1245 	} else {
1246 		oip->i_nlink--;
1247 		DIP_SET(oip, i_nlink, oip->i_nlink);
1248 		UFS_INODE_SET_FLAG(oip, IN_CHANGE);
1249 	}
1250 
1251 	error = UFS_BLKATOFF(vdp, (off_t)I_OFFSET(dp), (char **)&ep, &bp);
1252 	if (error == 0 && ep->d_namlen == 2 && ep->d_name[1] == '.' &&
1253 	    ep->d_name[0] == '.' && ep->d_ino != oip->i_number) {
1254 		brelse(bp);
1255 		error = EIDRM;
1256 	}
1257 	if (error) {
1258 		oip->i_effnlink++;
1259 		UFS_INODE_SET_FLAG(oip, IN_CHANGE);
1260 		if (DOINGSOFTDEP(vdp)) {
1261 			softdep_change_linkcnt(oip);
1262 		} else {
1263 			oip->i_nlink++;
1264 			DIP_SET(oip, i_nlink, oip->i_nlink);
1265 			UFS_INODE_SET_FLAG(oip, IN_CHANGE);
1266 		}
1267 		return (error);
1268 	}
1269 	ep->d_ino = newinum;
1270 	if (!OFSFMT(vdp))
1271 		ep->d_type = newtype;
1272 	if (DOINGSOFTDEP(vdp)) {
1273 		softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir);
1274 		bdwrite(bp);
1275 	} else {
1276 		if (DOINGASYNC(vdp)) {
1277 			bdwrite(bp);
1278 			error = 0;
1279 		} else {
1280 			error = bwrite(bp);
1281 		}
1282 	}
1283 	UFS_INODE_SET_FLAG(dp, IN_CHANGE | IN_UPDATE);
1284 	/*
1285 	 * If the last named reference to a snapshot goes away,
1286 	 * drop its snapshot reference so that it will be reclaimed
1287 	 * when last open reference goes away.
1288 	 */
1289 	if (IS_SNAPSHOT(oip) && oip->i_effnlink == 0)
1290 		UFS_SNAPGONE(oip);
1291 	return (error);
1292 }
1293 
1294 /*
1295  * Check if a directory is empty or not.
1296  * Inode supplied must be locked.
1297  *
1298  * Using a struct dirtemplate here is not precisely
1299  * what we want, but better than using a struct direct.
1300  *
1301  * NB: does not handle corrupted directories.
1302  */
1303 int
1304 ufs_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
1305 {
1306 	doff_t off;
1307 	struct dirtemplate dbuf;
1308 	struct direct *dp = (struct direct *)&dbuf;
1309 	int error, namlen;
1310 	ssize_t count;
1311 #define	MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1312 
1313 	for (off = 0; off < ip->i_size; off += dp->d_reclen) {
1314 		error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1315 		    off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1316 		    NOCRED, &count, (struct thread *)0);
1317 		/*
1318 		 * Since we read MINDIRSIZ, residual must
1319 		 * be 0 unless we're at end of file.
1320 		 */
1321 		if (error || count != 0)
1322 			return (0);
1323 		/* avoid infinite loops */
1324 		if (dp->d_reclen == 0)
1325 			return (0);
1326 		/* skip empty entries */
1327 		if (dp->d_ino == 0 || dp->d_ino == UFS_WINO)
1328 			continue;
1329 		/* accept only "." and ".." */
1330 #		if (BYTE_ORDER == LITTLE_ENDIAN)
1331 			if (OFSFMT(ITOV(ip)))
1332 				namlen = dp->d_type;
1333 			else
1334 				namlen = dp->d_namlen;
1335 #		else
1336 			namlen = dp->d_namlen;
1337 #		endif
1338 		if (namlen > 2)
1339 			return (0);
1340 		if (dp->d_name[0] != '.')
1341 			return (0);
1342 		/*
1343 		 * At this point namlen must be 1 or 2.
1344 		 * 1 implies ".", 2 implies ".." if second
1345 		 * char is also "."
1346 		 */
1347 		if (namlen == 1 && dp->d_ino == ip->i_number)
1348 			continue;
1349 		if (dp->d_name[1] == '.' && dp->d_ino == parentino)
1350 			continue;
1351 		return (0);
1352 	}
1353 	return (1);
1354 }
1355 
1356 static int
1357 ufs_dir_dd_ino(struct vnode *vp, struct ucred *cred, ino_t *dd_ino,
1358     struct vnode **dd_vp)
1359 {
1360 	struct dirtemplate dirbuf;
1361 	struct vnode *ddvp;
1362 	int error, namlen;
1363 
1364 	ASSERT_VOP_LOCKED(vp, "ufs_dir_dd_ino");
1365 	*dd_vp = NULL;
1366 	if (vp->v_type != VDIR)
1367 		return (ENOTDIR);
1368 	/*
1369 	 * First check to see if we have it in the name cache.
1370 	 */
1371 	if ((ddvp = vn_dir_dd_ino(vp)) != NULL) {
1372 		KASSERT(ddvp->v_mount == vp->v_mount,
1373 		    ("ufs_dir_dd_ino: Unexpected mount point crossing"));
1374 		*dd_ino = VTOI(ddvp)->i_number;
1375 		*dd_vp = ddvp;
1376 		return (0);
1377 	}
1378 	/*
1379 	 * Have to read the directory.
1380 	 */
1381 	error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1382 	    sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1383 	    IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL, NULL);
1384 	if (error != 0)
1385 		return (error);
1386 #if (BYTE_ORDER == LITTLE_ENDIAN)
1387 	if (OFSFMT(vp))
1388 		namlen = dirbuf.dotdot_type;
1389 	else
1390 		namlen = dirbuf.dotdot_namlen;
1391 #else
1392 	namlen = dirbuf.dotdot_namlen;
1393 #endif
1394 	if (namlen != 2 || dirbuf.dotdot_name[0] != '.' ||
1395 	    dirbuf.dotdot_name[1] != '.')
1396 		return (ENOTDIR);
1397 	*dd_ino = dirbuf.dotdot_ino;
1398 	return (0);
1399 }
1400 
1401 /*
1402  * Check if source directory is in the path of the target directory.
1403  */
1404 int
1405 ufs_checkpath(ino_t source_ino, ino_t parent_ino, struct inode *target,
1406     struct ucred *cred, ino_t *wait_ino)
1407 {
1408 	struct mount *mp;
1409 	struct vnode *tvp, *vp, *vp1;
1410 	int error;
1411 	ino_t dd_ino;
1412 
1413 	vp = tvp = ITOV(target);
1414 	mp = vp->v_mount;
1415 	*wait_ino = 0;
1416 	sx_assert(&VFSTOUFS(mp)->um_checkpath_lock, SA_XLOCKED);
1417 
1418 	if (target->i_number == source_ino)
1419 		return (EEXIST);
1420 	if (target->i_number == parent_ino)
1421 		return (0);
1422 	if (target->i_number == UFS_ROOTINO)
1423 		return (0);
1424 	for (;;) {
1425 		error = ufs_dir_dd_ino(vp, cred, &dd_ino, &vp1);
1426 		if (error != 0)
1427 			break;
1428 		if (dd_ino == source_ino) {
1429 			error = EINVAL;
1430 			break;
1431 		}
1432 		if (dd_ino == UFS_ROOTINO)
1433 			break;
1434 		if (dd_ino == parent_ino)
1435 			break;
1436 		if (vp1 == NULL) {
1437 			error = VFS_VGET(mp, dd_ino, LK_SHARED | LK_NOWAIT,
1438 			    &vp1);
1439 			if (error != 0) {
1440 				*wait_ino = dd_ino;
1441 				break;
1442 			}
1443 		}
1444 		KASSERT(dd_ino == VTOI(vp1)->i_number,
1445 		    ("directory %ju reparented\n",
1446 		    (uintmax_t)VTOI(vp1)->i_number));
1447 		if (vp != tvp)
1448 			vput(vp);
1449 		vp = vp1;
1450 	}
1451 
1452 	if (error == ENOTDIR)
1453 		panic("checkpath: .. not a directory\n");
1454 	if (vp1 != NULL)
1455 		vput(vp1);
1456 	if (vp != tvp)
1457 		vput(vp);
1458 	return (error);
1459 }
1460 
1461 #ifdef DIAGNOSTIC
1462 static void
1463 ufs_assert_inode_offset_owner(struct inode *ip, struct iown_tracker *tr,
1464     const char *name, const char *file, int line)
1465 {
1466 	char msg[128];
1467 
1468 	snprintf(msg, sizeof(msg), "at %s@%d", file, line);
1469 	ASSERT_VOP_ELOCKED(ITOV(ip), msg);
1470 	MPASS((ip->i_mode & IFMT) == IFDIR);
1471 	if (curthread == tr->tr_owner && ip->i_lock_gen == tr->tr_gen)
1472 		return;
1473 	printf("locked at\n");
1474 	stack_print(&tr->tr_st);
1475 	printf("unlocked at\n");
1476 	stack_print(&tr->tr_unlock);
1477 	panic("%s ip %p %jd offset owner %p %d gen %d "
1478 	    "curthread %p %d gen %d at %s@%d\n",
1479 	    name, ip, (uintmax_t)ip->i_number, tr->tr_owner,
1480 	    tr->tr_owner->td_tid, tr->tr_gen,
1481 	    curthread, curthread->td_tid, ip->i_lock_gen,
1482 	    file, line);
1483 }
1484 
1485 static void
1486 ufs_set_inode_offset_owner(struct inode *ip, struct iown_tracker *tr,
1487     const char *file, int line)
1488 {
1489 	char msg[128];
1490 
1491 	snprintf(msg, sizeof(msg), "at %s@%d", file, line);
1492 	ASSERT_VOP_ELOCKED(ITOV(ip), msg);
1493 	MPASS((ip->i_mode & IFMT) == IFDIR);
1494 	tr->tr_owner = curthread;
1495 	tr->tr_gen = ip->i_lock_gen;
1496 	stack_save(&tr->tr_st);
1497 }
1498 
1499 static void
1500 ufs_init_one_tracker(struct iown_tracker *tr)
1501 {
1502 	tr->tr_owner = NULL;
1503 	stack_zero(&tr->tr_st);
1504 }
1505 
1506 void
1507 ufs_init_trackers(struct inode *ip)
1508 {
1509 	ufs_init_one_tracker(&ip->i_offset_tracker);
1510 	ufs_init_one_tracker(&ip->i_count_tracker);
1511 	ufs_init_one_tracker(&ip->i_endoff_tracker);
1512 }
1513 
1514 void
1515 ufs_unlock_tracker(struct inode *ip)
1516 {
1517 	if (ip->i_count_tracker.tr_gen == ip->i_lock_gen)
1518 		stack_save(&ip->i_count_tracker.tr_unlock);
1519 	if (ip->i_offset_tracker.tr_gen == ip->i_lock_gen)
1520 		stack_save(&ip->i_offset_tracker.tr_unlock);
1521 	if (ip->i_endoff_tracker.tr_gen == ip->i_lock_gen)
1522 		stack_save(&ip->i_endoff_tracker.tr_unlock);
1523 	ip->i_lock_gen++;
1524 }
1525 
1526 doff_t
1527 ufs_get_i_offset(struct inode *ip, const char *file, int line)
1528 {
1529 	ufs_assert_inode_offset_owner(ip, &ip->i_offset_tracker, "i_offset",
1530 	    file, line);
1531 	return (ip->i_offset);
1532 }
1533 
1534 void
1535 ufs_set_i_offset(struct inode *ip, doff_t off, const char *file, int line)
1536 {
1537 	ufs_set_inode_offset_owner(ip, &ip->i_offset_tracker, file, line);
1538 	ip->i_offset = off;
1539 }
1540 
1541 int32_t
1542 ufs_get_i_count(struct inode *ip, const char *file, int line)
1543 {
1544 	ufs_assert_inode_offset_owner(ip, &ip->i_count_tracker, "i_count",
1545 	    file, line);
1546 	return (ip->i_count);
1547 }
1548 
1549 void
1550 ufs_set_i_count(struct inode *ip, int32_t cnt, const char *file, int line)
1551 {
1552 	ufs_set_inode_offset_owner(ip, &ip->i_count_tracker, file, line);
1553 	ip->i_count = cnt;
1554 }
1555 
1556 doff_t
1557 ufs_get_i_endoff(struct inode *ip, const char *file, int line)
1558 {
1559 	ufs_assert_inode_offset_owner(ip, &ip->i_endoff_tracker, "i_endoff",
1560 	    file, line);
1561 	return (ip->i_endoff);
1562 }
1563 
1564 void
1565 ufs_set_i_endoff(struct inode *ip, doff_t off, const char *file, int line)
1566 {
1567 	ufs_set_inode_offset_owner(ip, &ip->i_endoff_tracker, file, line);
1568 	ip->i_endoff = off;
1569 }
1570 
1571 #endif
1572