xref: /original-bsd/sys/ufs/ufs/ufs_lookup.c (revision 0ac4996f)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)ufs_lookup.c	8.12 (Berkeley) 05/14/95
13  */
14 
15 #include <sys/param.h>
16 #include <sys/namei.h>
17 #include <sys/buf.h>
18 #include <sys/file.h>
19 #include <sys/mount.h>
20 #include <sys/vnode.h>
21 
22 #include <ufs/ufs/quota.h>
23 #include <ufs/ufs/inode.h>
24 #include <ufs/ufs/dir.h>
25 #include <ufs/ufs/ufsmount.h>
26 #include <ufs/ufs/ufs_extern.h>
27 
28 struct	nchstats nchstats;
29 #ifdef DIAGNOSTIC
30 int	dirchk = 1;
31 #else
32 int	dirchk = 0;
33 #endif
34 
35 #define FSFMT(vp)	((vp)->v_mount->mnt_maxsymlinklen <= 0)
36 
37 /*
38  * Convert a component of a pathname into a pointer to a locked inode.
39  * This is a very central and rather complicated routine.
40  * If the file system is not maintained in a strict tree hierarchy,
41  * this can result in a deadlock situation (see comments in code below).
42  *
43  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
44  * on whether the name is to be looked up, created, renamed, or deleted.
45  * When CREATE, RENAME, or DELETE is specified, information usable in
46  * creating, renaming, or deleting a directory entry may be calculated.
47  * If flag has LOCKPARENT or'ed into it and the target of the pathname
48  * exists, lookup returns both the target and its parent directory locked.
49  * When creating or renaming and LOCKPARENT is specified, the target may
50  * not be ".".  When deleting and LOCKPARENT is specified, the target may
51  * be "."., but the caller must check to ensure it does an vrele and vput
52  * instead of two vputs.
53  *
54  * Overall outline of ufs_lookup:
55  *
56  *	check accessibility of directory
57  *	look for name in cache, if found, then if at end of path
58  *	  and deleting or creating, drop it, else return name
59  *	search for name in directory, to found or notfound
60  * notfound:
61  *	if creating, return locked directory, leaving info on available slots
62  *	else return error
63  * found:
64  *	if at end of path and deleting, return information to allow delete
65  *	if at end of path and rewriting (RENAME and LOCKPARENT), lock target
66  *	  inode and return info to allow rewrite
67  *	if not at end, add name to cache; if at end and neither creating
68  *	  nor deleting, add name to cache
69  */
70 int
71 ufs_lookup(ap)
72 	struct vop_lookup_args /* {
73 		struct vnode *a_dvp;
74 		struct vnode **a_vpp;
75 		struct componentname *a_cnp;
76 	} */ *ap;
77 {
78 	register struct vnode *vdp;	/* vnode for directory being searched */
79 	register struct inode *dp;	/* inode for directory being searched */
80 	struct buf *bp;			/* a buffer of directory entries */
81 	register struct direct *ep;	/* the current directory entry */
82 	int entryoffsetinblock;		/* offset of ep in bp's buffer */
83 	enum {NONE, COMPACT, FOUND} slotstatus;
84 	doff_t slotoffset;		/* offset of area with free space */
85 	int slotsize;			/* size of area at slotoffset */
86 	int slotfreespace;		/* amount of space free in slot */
87 	int slotneeded;			/* size of the entry we're seeking */
88 	int numdirpasses;		/* strategy for directory search */
89 	doff_t endsearch;		/* offset to end directory search */
90 	doff_t prevoff;			/* prev entry dp->i_offset */
91 	struct vnode *pdp;		/* saved dp during symlink work */
92 	struct vnode *tdp;		/* returned by VFS_VGET */
93 	doff_t enduseful;		/* pointer past last used dir slot */
94 	u_long bmask;			/* block offset mask */
95 	int lockparent;			/* 1 => lockparent flag is set */
96 	int wantparent;			/* 1 => wantparent or lockparent flag */
97 	int namlen, error;
98 	struct vnode **vpp = ap->a_vpp;
99 	struct componentname *cnp = ap->a_cnp;
100 	struct ucred *cred = cnp->cn_cred;
101 	int flags = cnp->cn_flags;
102 	int nameiop = cnp->cn_nameiop;
103 	struct proc *p = cnp->cn_proc;
104 
105 	bp = NULL;
106 	slotoffset = -1;
107 	*vpp = NULL;
108 	vdp = ap->a_dvp;
109 	dp = VTOI(vdp);
110 	lockparent = flags & LOCKPARENT;
111 	wantparent = flags & (LOCKPARENT|WANTPARENT);
112 
113 	/*
114 	 * Check accessiblity of directory.
115 	 */
116 	if ((dp->i_mode & IFMT) != IFDIR)
117 		return (ENOTDIR);
118 	if (error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc))
119 		return (error);
120 
121 	/*
122 	 * We now have a segment name to search for, and a directory to search.
123 	 *
124 	 * Before tediously performing a linear scan of the directory,
125 	 * check the name cache to see if the directory/name pair
126 	 * we are looking for is known already.
127 	 */
128 	if (error = cache_lookup(vdp, vpp, cnp)) {
129 		int vpid;	/* capability number of vnode */
130 
131 		if (error == ENOENT)
132 			return (error);
133 		/*
134 		 * Get the next vnode in the path.
135 		 * See comment below starting `Step through' for
136 		 * an explaination of the locking protocol.
137 		 */
138 		pdp = vdp;
139 		dp = VTOI(*vpp);
140 		vdp = *vpp;
141 		vpid = vdp->v_id;
142 		if (pdp == vdp) {   /* lookup on "." */
143 			VREF(vdp);
144 			error = 0;
145 		} else if (flags & ISDOTDOT) {
146 			VOP_UNLOCK(pdp, 0, p);
147 			error = vget(vdp, LK_EXCLUSIVE, p);
148 			if (!error && lockparent && (flags & ISLASTCN))
149 				error = vn_lock(pdp, LK_EXCLUSIVE, p);
150 		} else {
151 			error = vget(vdp, LK_EXCLUSIVE, p);
152 			if (!lockparent || error || !(flags & ISLASTCN))
153 				VOP_UNLOCK(pdp, 0, p);
154 		}
155 		/*
156 		 * Check that the capability number did not change
157 		 * while we were waiting for the lock.
158 		 */
159 		if (!error) {
160 			if (vpid == vdp->v_id)
161 				return (0);
162 			vput(vdp);
163 			if (lockparent && pdp != vdp && (flags & ISLASTCN))
164 				VOP_UNLOCK(pdp, 0, p);
165 		}
166 		if (error = vn_lock(pdp, LK_EXCLUSIVE, p))
167 			return (error);
168 		vdp = pdp;
169 		dp = VTOI(pdp);
170 		*vpp = NULL;
171 	}
172 
173 	/*
174 	 * Suppress search for slots unless creating
175 	 * file and at end of pathname, in which case
176 	 * we watch for a place to put the new file in
177 	 * case it doesn't already exist.
178 	 */
179 	slotstatus = FOUND;
180 	slotfreespace = slotsize = slotneeded = 0;
181 	if ((nameiop == CREATE || nameiop == RENAME) &&
182 	    (flags & ISLASTCN)) {
183 		slotstatus = NONE;
184 		slotneeded = (sizeof(struct direct) - MAXNAMLEN +
185 			cnp->cn_namelen + 3) &~ 3;
186 	}
187 
188 	/*
189 	 * If there is cached information on a previous search of
190 	 * this directory, pick up where we last left off.
191 	 * We cache only lookups as these are the most common
192 	 * and have the greatest payoff. Caching CREATE has little
193 	 * benefit as it usually must search the entire directory
194 	 * to determine that the entry does not exist. Caching the
195 	 * location of the last DELETE or RENAME has not reduced
196 	 * profiling time and hence has been removed in the interest
197 	 * of simplicity.
198 	 */
199 	bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
200 	if (nameiop != LOOKUP || dp->i_diroff == 0 ||
201 	    dp->i_diroff > dp->i_size) {
202 		entryoffsetinblock = 0;
203 		dp->i_offset = 0;
204 		numdirpasses = 1;
205 	} else {
206 		dp->i_offset = dp->i_diroff;
207 		if ((entryoffsetinblock = dp->i_offset & bmask) &&
208 		    (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
209 			return (error);
210 		numdirpasses = 2;
211 		nchstats.ncs_2passes++;
212 	}
213 	prevoff = dp->i_offset;
214 	endsearch = roundup(dp->i_size, DIRBLKSIZ);
215 	enduseful = 0;
216 
217 searchloop:
218 	while (dp->i_offset < endsearch) {
219 		/*
220 		 * If necessary, get the next directory block.
221 		 */
222 		if ((dp->i_offset & bmask) == 0) {
223 			if (bp != NULL)
224 				brelse(bp);
225 			if (error =
226 			    VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp))
227 				return (error);
228 			entryoffsetinblock = 0;
229 		}
230 		/*
231 		 * If still looking for a slot, and at a DIRBLKSIZE
232 		 * boundary, have to start looking for free space again.
233 		 */
234 		if (slotstatus == NONE &&
235 		    (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
236 			slotoffset = -1;
237 			slotfreespace = 0;
238 		}
239 		/*
240 		 * Get pointer to next entry.
241 		 * Full validation checks are slow, so we only check
242 		 * enough to insure forward progress through the
243 		 * directory. Complete checks can be run by patching
244 		 * "dirchk" to be true.
245 		 */
246 		ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
247 		if (ep->d_reclen == 0 ||
248 		    dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock)) {
249 			int i;
250 
251 			ufs_dirbad(dp, dp->i_offset, "mangled entry");
252 			i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
253 			dp->i_offset += i;
254 			entryoffsetinblock += i;
255 			continue;
256 		}
257 
258 		/*
259 		 * If an appropriate sized slot has not yet been found,
260 		 * check to see if one is available. Also accumulate space
261 		 * in the current block so that we can determine if
262 		 * compaction is viable.
263 		 */
264 		if (slotstatus != FOUND) {
265 			int size = ep->d_reclen;
266 
267 			if (ep->d_ino != 0)
268 				size -= DIRSIZ(FSFMT(vdp), ep);
269 			if (size > 0) {
270 				if (size >= slotneeded) {
271 					slotstatus = FOUND;
272 					slotoffset = dp->i_offset;
273 					slotsize = ep->d_reclen;
274 				} else if (slotstatus == NONE) {
275 					slotfreespace += size;
276 					if (slotoffset == -1)
277 						slotoffset = dp->i_offset;
278 					if (slotfreespace >= slotneeded) {
279 						slotstatus = COMPACT;
280 						slotsize = dp->i_offset +
281 						      ep->d_reclen - slotoffset;
282 					}
283 				}
284 			}
285 		}
286 
287 		/*
288 		 * Check for a name match.
289 		 */
290 		if (ep->d_ino) {
291 #			if (BYTE_ORDER == LITTLE_ENDIAN)
292 				if (vdp->v_mount->mnt_maxsymlinklen > 0)
293 					namlen = ep->d_namlen;
294 				else
295 					namlen = ep->d_type;
296 #			else
297 				namlen = ep->d_namlen;
298 #			endif
299 			if (namlen == cnp->cn_namelen &&
300 			    !bcmp(cnp->cn_nameptr, ep->d_name,
301 				(unsigned)namlen)) {
302 				/*
303 				 * Save directory entry's inode number and
304 				 * reclen in ndp->ni_ufs area, and release
305 				 * directory buffer.
306 				 */
307 				if (vdp->v_mount->mnt_maxsymlinklen > 0 &&
308 				    ep->d_type == DT_WHT) {
309 					slotstatus = FOUND;
310 					slotoffset = dp->i_offset;
311 					slotsize = ep->d_reclen;
312 					dp->i_reclen = slotsize;
313 					enduseful = slotoffset + slotsize;
314 					ap->a_cnp->cn_flags |= ISWHITEOUT;
315 					numdirpasses--;
316 					goto notfound;
317 				}
318 				dp->i_ino = ep->d_ino;
319 				dp->i_reclen = ep->d_reclen;
320 				brelse(bp);
321 				goto found;
322 			}
323 		}
324 		prevoff = dp->i_offset;
325 		dp->i_offset += ep->d_reclen;
326 		entryoffsetinblock += ep->d_reclen;
327 		if (ep->d_ino)
328 			enduseful = dp->i_offset;
329 	}
330 notfound:
331 	/*
332 	 * If we started in the middle of the directory and failed
333 	 * to find our target, we must check the beginning as well.
334 	 */
335 	if (numdirpasses == 2) {
336 		numdirpasses--;
337 		dp->i_offset = 0;
338 		endsearch = dp->i_diroff;
339 		goto searchloop;
340 	}
341 	if (bp != NULL)
342 		brelse(bp);
343 	/*
344 	 * If creating, and at end of pathname and current
345 	 * directory has not been removed, then can consider
346 	 * allowing file to be created.
347 	 */
348 	if ((nameiop == CREATE || nameiop == RENAME ||
349 	     (nameiop == DELETE &&
350 	      (ap->a_cnp->cn_flags & DOWHITEOUT) &&
351 	      (ap->a_cnp->cn_flags & ISWHITEOUT))) &&
352 	    (flags & ISLASTCN) && dp->i_nlink != 0) {
353 		/*
354 		 * Access for write is interpreted as allowing
355 		 * creation of files in the directory.
356 		 */
357 		if (error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc))
358 			return (error);
359 		/*
360 		 * Return an indication of where the new directory
361 		 * entry should be put.  If we didn't find a slot,
362 		 * then set dp->i_count to 0 indicating
363 		 * that the new slot belongs at the end of the
364 		 * directory. If we found a slot, then the new entry
365 		 * can be put in the range from dp->i_offset to
366 		 * dp->i_offset + dp->i_count.
367 		 */
368 		if (slotstatus == NONE) {
369 			dp->i_offset = roundup(dp->i_size, DIRBLKSIZ);
370 			dp->i_count = 0;
371 			enduseful = dp->i_offset;
372 		} else if (nameiop == DELETE) {
373 			dp->i_offset = slotoffset;
374 			if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
375 				dp->i_count = 0;
376 			else
377 				dp->i_count = dp->i_offset - prevoff;
378 		} else {
379 			dp->i_offset = slotoffset;
380 			dp->i_count = slotsize;
381 			if (enduseful < slotoffset + slotsize)
382 				enduseful = slotoffset + slotsize;
383 		}
384 		dp->i_endoff = roundup(enduseful, DIRBLKSIZ);
385 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
386 		/*
387 		 * We return with the directory locked, so that
388 		 * the parameters we set up above will still be
389 		 * valid if we actually decide to do a direnter().
390 		 * We return ni_vp == NULL to indicate that the entry
391 		 * does not currently exist; we leave a pointer to
392 		 * the (locked) directory inode in ndp->ni_dvp.
393 		 * The pathname buffer is saved so that the name
394 		 * can be obtained later.
395 		 *
396 		 * NB - if the directory is unlocked, then this
397 		 * information cannot be used.
398 		 */
399 		cnp->cn_flags |= SAVENAME;
400 		if (!lockparent)
401 			VOP_UNLOCK(vdp, 0, p);
402 		return (EJUSTRETURN);
403 	}
404 	/*
405 	 * Insert name into cache (as non-existent) if appropriate.
406 	 */
407 	if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
408 		cache_enter(vdp, *vpp, cnp);
409 	return (ENOENT);
410 
411 found:
412 	if (numdirpasses == 2)
413 		nchstats.ncs_pass2++;
414 	/*
415 	 * Check that directory length properly reflects presence
416 	 * of this entry.
417 	 */
418 	if (entryoffsetinblock + DIRSIZ(FSFMT(vdp), ep) > dp->i_size) {
419 		ufs_dirbad(dp, dp->i_offset, "i_size too small");
420 		dp->i_size = entryoffsetinblock + DIRSIZ(FSFMT(vdp), ep);
421 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
422 	}
423 
424 	/*
425 	 * Found component in pathname.
426 	 * If the final component of path name, save information
427 	 * in the cache as to where the entry was found.
428 	 */
429 	if ((flags & ISLASTCN) && nameiop == LOOKUP)
430 		dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
431 
432 	/*
433 	 * If deleting, and at end of pathname, return
434 	 * parameters which can be used to remove file.
435 	 * If the wantparent flag isn't set, we return only
436 	 * the directory (in ndp->ni_dvp), otherwise we go
437 	 * on and lock the inode, being careful with ".".
438 	 */
439 	if (nameiop == DELETE && (flags & ISLASTCN)) {
440 		/*
441 		 * Write access to directory required to delete files.
442 		 */
443 		if (error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc))
444 			return (error);
445 		/*
446 		 * Return pointer to current entry in dp->i_offset,
447 		 * and distance past previous entry (if there
448 		 * is a previous entry in this block) in dp->i_count.
449 		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
450 		 */
451 		if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
452 			dp->i_count = 0;
453 		else
454 			dp->i_count = dp->i_offset - prevoff;
455 		if (dp->i_number == dp->i_ino) {
456 			VREF(vdp);
457 			*vpp = vdp;
458 			return (0);
459 		}
460 		if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp))
461 			return (error);
462 		/*
463 		 * If directory is "sticky", then user must own
464 		 * the directory, or the file in it, else she
465 		 * may not delete it (unless she's root). This
466 		 * implements append-only directories.
467 		 */
468 		if ((dp->i_mode & ISVTX) &&
469 		    cred->cr_uid != 0 &&
470 		    cred->cr_uid != dp->i_uid &&
471 		    VTOI(tdp)->i_uid != cred->cr_uid) {
472 			vput(tdp);
473 			return (EPERM);
474 		}
475 		*vpp = tdp;
476 		if (!lockparent)
477 			VOP_UNLOCK(vdp, 0, p);
478 		return (0);
479 	}
480 
481 	/*
482 	 * If rewriting (RENAME), return the inode and the
483 	 * information required to rewrite the present directory
484 	 * Must get inode of directory entry to verify it's a
485 	 * regular file, or empty directory.
486 	 */
487 	if (nameiop == RENAME && wantparent &&
488 	    (flags & ISLASTCN)) {
489 		if (error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc))
490 			return (error);
491 		/*
492 		 * Careful about locking second inode.
493 		 * This can only occur if the target is ".".
494 		 */
495 		if (dp->i_number == dp->i_ino)
496 			return (EISDIR);
497 		if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp))
498 			return (error);
499 		*vpp = tdp;
500 		cnp->cn_flags |= SAVENAME;
501 		if (!lockparent)
502 			VOP_UNLOCK(vdp, 0, p);
503 		return (0);
504 	}
505 
506 	/*
507 	 * Step through the translation in the name.  We do not `vput' the
508 	 * directory because we may need it again if a symbolic link
509 	 * is relative to the current directory.  Instead we save it
510 	 * unlocked as "pdp".  We must get the target inode before unlocking
511 	 * the directory to insure that the inode will not be removed
512 	 * before we get it.  We prevent deadlock by always fetching
513 	 * inodes from the root, moving down the directory tree. Thus
514 	 * when following backward pointers ".." we must unlock the
515 	 * parent directory before getting the requested directory.
516 	 * There is a potential race condition here if both the current
517 	 * and parent directories are removed before the VFS_VGET for the
518 	 * inode associated with ".." returns.  We hope that this occurs
519 	 * infrequently since we cannot avoid this race condition without
520 	 * implementing a sophisticated deadlock detection algorithm.
521 	 * Note also that this simple deadlock detection scheme will not
522 	 * work if the file system has any hard links other than ".."
523 	 * that point backwards in the directory structure.
524 	 */
525 	pdp = vdp;
526 	if (flags & ISDOTDOT) {
527 		VOP_UNLOCK(pdp, 0, p);	/* race to get the inode */
528 		if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) {
529 			vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, p);
530 			return (error);
531 		}
532 		if (lockparent && (flags & ISLASTCN) &&
533 		    (error = vn_lock(pdp, LK_EXCLUSIVE, p))) {
534 			vput(tdp);
535 			return (error);
536 		}
537 		*vpp = tdp;
538 	} else if (dp->i_number == dp->i_ino) {
539 		VREF(vdp);	/* we want ourself, ie "." */
540 		*vpp = vdp;
541 	} else {
542 		if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp))
543 			return (error);
544 		if (!lockparent || !(flags & ISLASTCN))
545 			VOP_UNLOCK(pdp, 0, p);
546 		*vpp = tdp;
547 	}
548 
549 	/*
550 	 * Insert name into cache if appropriate.
551 	 */
552 	if (cnp->cn_flags & MAKEENTRY)
553 		cache_enter(vdp, *vpp, cnp);
554 	return (0);
555 }
556 
557 void
558 ufs_dirbad(ip, offset, how)
559 	struct inode *ip;
560 	doff_t offset;
561 	char *how;
562 {
563 	struct mount *mp;
564 
565 	mp = ITOV(ip)->v_mount;
566 	(void)printf("%s: bad dir ino %d at offset %d: %s\n",
567 	    mp->mnt_stat.f_mntonname, ip->i_number, offset, how);
568 	if ((mp->mnt_stat.f_flags & MNT_RDONLY) == 0)
569 		panic("bad dir");
570 }
571 
572 /*
573  * Do consistency checking on a directory entry:
574  *	record length must be multiple of 4
575  *	entry must fit in rest of its DIRBLKSIZ block
576  *	record must be large enough to contain entry
577  *	name is not longer than MAXNAMLEN
578  *	name must be as long as advertised, and null terminated
579  */
580 int
581 ufs_dirbadentry(dp, ep, entryoffsetinblock)
582 	struct vnode *dp;
583 	register struct direct *ep;
584 	int entryoffsetinblock;
585 {
586 	register int i;
587 	int namlen;
588 
589 #	if (BYTE_ORDER == LITTLE_ENDIAN)
590 		if (dp->v_mount->mnt_maxsymlinklen > 0)
591 			namlen = ep->d_namlen;
592 		else
593 			namlen = ep->d_type;
594 #	else
595 		namlen = ep->d_namlen;
596 #	endif
597 	if ((ep->d_reclen & 0x3) != 0 ||
598 	    ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
599 	    ep->d_reclen < DIRSIZ(FSFMT(dp), ep) || namlen > MAXNAMLEN) {
600 		/*return (1); */
601 		printf("First bad\n");
602 		goto bad;
603 	}
604 	if (ep->d_ino == 0)
605 		return (0);
606 	for (i = 0; i < namlen; i++)
607 		if (ep->d_name[i] == '\0') {
608 			/*return (1); */
609 			printf("Second bad\n");
610 			goto bad;
611 	}
612 	if (ep->d_name[i])
613 		goto bad;
614 	return (0);
615 bad:
616 	return (1);
617 }
618 
619 /*
620  * Write a directory entry after a call to namei, using the parameters
621  * that it left in nameidata.  The argument ip is the inode which the new
622  * directory entry will refer to.  Dvp is a pointer to the directory to
623  * be written, which was left locked by namei. Remaining parameters
624  * (dp->i_offset, dp->i_count) indicate how the space for the new
625  * entry is to be obtained.
626  */
627 int
628 ufs_direnter(ip, dvp, cnp)
629 	struct inode *ip;
630 	struct vnode *dvp;
631 	register struct componentname *cnp;
632 {
633 	register struct inode *dp;
634 	struct direct newdir;
635 
636 #ifdef DIAGNOSTIC
637 	if ((cnp->cn_flags & SAVENAME) == 0)
638 		panic("direnter: missing name");
639 #endif
640 	dp = VTOI(dvp);
641 	newdir.d_ino = ip->i_number;
642 	newdir.d_namlen = cnp->cn_namelen;
643 	bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
644 	if (dvp->v_mount->mnt_maxsymlinklen > 0)
645 		newdir.d_type = IFTODT(ip->i_mode);
646 	else {
647 		newdir.d_type = 0;
648 #		if (BYTE_ORDER == LITTLE_ENDIAN)
649 			{ u_char tmp = newdir.d_namlen;
650 			newdir.d_namlen = newdir.d_type;
651 			newdir.d_type = tmp; }
652 #		endif
653 	}
654 	return (ufs_direnter2(dvp, &newdir, cnp->cn_cred, cnp->cn_proc));
655 }
656 
657 /*
658  * Common entry point for directory entry removal used by ufs_direnter
659  * and ufs_whiteout
660  */
661 ufs_direnter2(dvp, dirp, cr, p)
662 	struct vnode *dvp;
663 	struct direct *dirp;
664 	struct ucred *cr;
665 	struct proc *p;
666 {
667 	int newentrysize;
668 	struct inode *dp;
669 	struct buf *bp;
670 	struct iovec aiov;
671 	struct uio auio;
672 	u_int dsize;
673 	struct direct *ep, *nep;
674 	int error, loc, spacefree;
675 	char *dirbuf;
676 
677 	dp = VTOI(dvp);
678 	newentrysize = DIRSIZ(FSFMT(dvp), dirp);
679 
680 	if (dp->i_count == 0) {
681 		/*
682 		 * If dp->i_count is 0, then namei could find no
683 		 * space in the directory. Here, dp->i_offset will
684 		 * be on a directory block boundary and we will write the
685 		 * new entry into a fresh block.
686 		 */
687 		if (dp->i_offset & (DIRBLKSIZ - 1))
688 			panic("ufs_direnter2: newblk");
689 		auio.uio_offset = dp->i_offset;
690 		dirp->d_reclen = DIRBLKSIZ;
691 		auio.uio_resid = newentrysize;
692 		aiov.iov_len = newentrysize;
693 		aiov.iov_base = (caddr_t)dirp;
694 		auio.uio_iov = &aiov;
695 		auio.uio_iovcnt = 1;
696 		auio.uio_rw = UIO_WRITE;
697 		auio.uio_segflg = UIO_SYSSPACE;
698 		auio.uio_procp = (struct proc *)0;
699 		error = VOP_WRITE(dvp, &auio, IO_SYNC, cr);
700 		if (DIRBLKSIZ >
701 		    VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
702 			/* XXX should grow with balloc() */
703 			panic("ufs_direnter2: frag size");
704 		else if (!error) {
705 			dp->i_size = roundup(dp->i_size, DIRBLKSIZ);
706 			dp->i_flag |= IN_CHANGE;
707 		}
708 		return (error);
709 	}
710 
711 	/*
712 	 * If dp->i_count is non-zero, then namei found space
713 	 * for the new entry in the range dp->i_offset to
714 	 * dp->i_offset + dp->i_count in the directory.
715 	 * To use this space, we may have to compact the entries located
716 	 * there, by copying them together towards the beginning of the
717 	 * block, leaving the free space in one usable chunk at the end.
718 	 */
719 
720 	/*
721 	 * Increase size of directory if entry eats into new space.
722 	 * This should never push the size past a new multiple of
723 	 * DIRBLKSIZE.
724 	 *
725 	 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
726 	 */
727 	if (dp->i_offset + dp->i_count > dp->i_size)
728 		dp->i_size = dp->i_offset + dp->i_count;
729 	/*
730 	 * Get the block containing the space for the new directory entry.
731 	 */
732 	if (error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp))
733 		return (error);
734 	/*
735 	 * Find space for the new entry. In the simple case, the entry at
736 	 * offset base will have the space. If it does not, then namei
737 	 * arranged that compacting the region dp->i_offset to
738 	 * dp->i_offset + dp->i_count would yield the
739 	 * space.
740 	 */
741 	ep = (struct direct *)dirbuf;
742 	dsize = DIRSIZ(FSFMT(dvp), ep);
743 	spacefree = ep->d_reclen - dsize;
744 	for (loc = ep->d_reclen; loc < dp->i_count; ) {
745 		nep = (struct direct *)(dirbuf + loc);
746 		if (ep->d_ino) {
747 			/* trim the existing slot */
748 			ep->d_reclen = dsize;
749 			ep = (struct direct *)((char *)ep + dsize);
750 		} else {
751 			/* overwrite; nothing there; header is ours */
752 			spacefree += dsize;
753 		}
754 		dsize = DIRSIZ(FSFMT(dvp), nep);
755 		spacefree += nep->d_reclen - dsize;
756 		loc += nep->d_reclen;
757 		bcopy((caddr_t)nep, (caddr_t)ep, dsize);
758 	}
759 	/*
760 	 * Update the pointer fields in the previous entry (if any),
761 	 * copy in the new entry, and write out the block.
762 	 */
763 	if (ep->d_ino == 0 ||
764 	    (ep->d_ino == WINO &&
765 	     bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
766 		if (spacefree + dsize < newentrysize)
767 			panic("ufs_direnter2: compact1");
768 		dirp->d_reclen = spacefree + dsize;
769 	} else {
770 		if (spacefree < newentrysize)
771 			panic("ufs_direnter2: compact2");
772 		dirp->d_reclen = spacefree;
773 		ep->d_reclen = dsize;
774 		ep = (struct direct *)((char *)ep + dsize);
775 	}
776 	bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize);
777 	error = VOP_BWRITE(bp);
778 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
779 	if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
780 		error = VOP_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC, cr, p);
781 	return (error);
782 }
783 
784 /*
785  * Remove a directory entry after a call to namei, using
786  * the parameters which it left in nameidata. The entry
787  * dp->i_offset contains the offset into the directory of the
788  * entry to be eliminated.  The dp->i_count field contains the
789  * size of the previous record in the directory.  If this
790  * is 0, the first entry is being deleted, so we need only
791  * zero the inode number to mark the entry as free.  If the
792  * entry is not the first in the directory, we must reclaim
793  * the space of the now empty record by adding the record size
794  * to the size of the previous entry.
795  */
796 int
797 ufs_dirremove(dvp, cnp)
798 	struct vnode *dvp;
799 	struct componentname *cnp;
800 {
801 	register struct inode *dp;
802 	struct direct *ep;
803 	struct buf *bp;
804 	int error;
805 
806 	dp = VTOI(dvp);
807 
808 	if (cnp->cn_flags & DOWHITEOUT) {
809 		/*
810 		 * Whiteout entry: set d_ino to WINO.
811 		 */
812 		if (error =
813 		    VOP_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp))
814 			return (error);
815 		ep->d_ino = WINO;
816 		ep->d_type = DT_WHT;
817 		error = VOP_BWRITE(bp);
818 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
819 		return (error);
820 	}
821 
822 	if (dp->i_count == 0) {
823 		/*
824 		 * First entry in block: set d_ino to zero.
825 		 */
826 		if (error =
827 		    VOP_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp))
828 			return (error);
829 		ep->d_ino = 0;
830 		error = VOP_BWRITE(bp);
831 		dp->i_flag |= IN_CHANGE | IN_UPDATE;
832 		return (error);
833 	}
834 	/*
835 	 * Collapse new free space into previous entry.
836 	 */
837 	if (error = VOP_BLKATOFF(dvp, (off_t)(dp->i_offset - dp->i_count),
838 	    (char **)&ep, &bp))
839 		return (error);
840 	ep->d_reclen += dp->i_reclen;
841 	error = VOP_BWRITE(bp);
842 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
843 	return (error);
844 }
845 
846 /*
847  * Rewrite an existing directory entry to point at the inode
848  * supplied.  The parameters describing the directory entry are
849  * set up by a call to namei.
850  */
851 int
852 ufs_dirrewrite(dp, ip, cnp)
853 	struct inode *dp, *ip;
854 	struct componentname *cnp;
855 {
856 	struct buf *bp;
857 	struct direct *ep;
858 	struct vnode *vdp = ITOV(dp);
859 	int error;
860 
861 	if (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp))
862 		return (error);
863 	ep->d_ino = ip->i_number;
864 	if (vdp->v_mount->mnt_maxsymlinklen > 0)
865 		ep->d_type = IFTODT(ip->i_mode);
866 	error = VOP_BWRITE(bp);
867 	dp->i_flag |= IN_CHANGE | IN_UPDATE;
868 	return (error);
869 }
870 
871 /*
872  * Check if a directory is empty or not.
873  * Inode supplied must be locked.
874  *
875  * Using a struct dirtemplate here is not precisely
876  * what we want, but better than using a struct direct.
877  *
878  * NB: does not handle corrupted directories.
879  */
880 int
881 ufs_dirempty(ip, parentino, cred)
882 	register struct inode *ip;
883 	ino_t parentino;
884 	struct ucred *cred;
885 {
886 	register off_t off;
887 	struct dirtemplate dbuf;
888 	register struct direct *dp = (struct direct *)&dbuf;
889 	int error, count, namlen;
890 #define	MINDIRSIZ (sizeof (struct dirtemplate) / 2)
891 
892 	for (off = 0; off < ip->i_size; off += dp->d_reclen) {
893 		error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
894 		   UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0);
895 		/*
896 		 * Since we read MINDIRSIZ, residual must
897 		 * be 0 unless we're at end of file.
898 		 */
899 		if (error || count != 0)
900 			return (0);
901 		/* avoid infinite loops */
902 		if (dp->d_reclen == 0)
903 			return (0);
904 		/* skip empty entries */
905 		if (dp->d_ino == 0 || dp->d_ino == WINO)
906 			continue;
907 		/* accept only "." and ".." */
908 #		if (BYTE_ORDER == LITTLE_ENDIAN)
909 			if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0)
910 				namlen = dp->d_namlen;
911 			else
912 				namlen = dp->d_type;
913 #		else
914 			namlen = dp->d_namlen;
915 #		endif
916 		if (namlen > 2)
917 			return (0);
918 		if (dp->d_name[0] != '.')
919 			return (0);
920 		/*
921 		 * At this point namlen must be 1 or 2.
922 		 * 1 implies ".", 2 implies ".." if second
923 		 * char is also "."
924 		 */
925 		if (namlen == 1)
926 			continue;
927 		if (dp->d_name[1] == '.' && dp->d_ino == parentino)
928 			continue;
929 		return (0);
930 	}
931 	return (1);
932 }
933 
934 /*
935  * Check if source directory is in the path of the target directory.
936  * Target is supplied locked, source is unlocked.
937  * The target is always vput before returning.
938  */
939 int
940 ufs_checkpath(source, target, cred)
941 	struct inode *source, *target;
942 	struct ucred *cred;
943 {
944 	struct vnode *vp;
945 	int error, rootino, namlen;
946 	struct dirtemplate dirbuf;
947 
948 	vp = ITOV(target);
949 	if (target->i_number == source->i_number) {
950 		error = EEXIST;
951 		goto out;
952 	}
953 	rootino = ROOTINO;
954 	error = 0;
955 	if (target->i_number == rootino)
956 		goto out;
957 
958 	for (;;) {
959 		if (vp->v_type != VDIR) {
960 			error = ENOTDIR;
961 			break;
962 		}
963 		error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
964 			sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
965 			IO_NODELOCKED, cred, (int *)0, (struct proc *)0);
966 		if (error != 0)
967 			break;
968 #		if (BYTE_ORDER == LITTLE_ENDIAN)
969 			if (vp->v_mount->mnt_maxsymlinklen > 0)
970 				namlen = dirbuf.dotdot_namlen;
971 			else
972 				namlen = dirbuf.dotdot_type;
973 #		else
974 			namlen = dirbuf.dotdot_namlen;
975 #		endif
976 		if (namlen != 2 ||
977 		    dirbuf.dotdot_name[0] != '.' ||
978 		    dirbuf.dotdot_name[1] != '.') {
979 			error = ENOTDIR;
980 			break;
981 		}
982 		if (dirbuf.dotdot_ino == source->i_number) {
983 			error = EINVAL;
984 			break;
985 		}
986 		if (dirbuf.dotdot_ino == rootino)
987 			break;
988 		vput(vp);
989 		if (error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino, &vp)) {
990 			vp = NULL;
991 			break;
992 		}
993 	}
994 
995 out:
996 	if (error == ENOTDIR)
997 		printf("checkpath: .. not a directory\n");
998 	if (vp != NULL)
999 		vput(vp);
1000 	return (error);
1001 }
1002