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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
39 * $FreeBSD: src/sys/ufs/ufs/ufs_lookup.c,v 1.33.2.7 2001/09/22 19:22:13 iedowse Exp $
40 * $DragonFly: src/sys/vfs/ufs/ufs_lookup.c,v 1.28 2006/12/23 00:41:30 swildner Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
50 #include <sys/namei.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/sysctl.h>
57 #include <vm/vm_extern.h>
66 #include "ufs_extern.h"
67 #include "ffs_extern.h"
75 SYSCTL_INT(_debug
, OID_AUTO
, dircheck
, CTLFLAG_RW
, &dirchk
, 0, "");
77 /* true if old FS format...*/
78 #define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
81 * Convert a component of a pathname into a pointer to a locked inode.
82 * This is a very central and rather complicated routine.
83 * If the filesystem is not maintained in a strict tree hierarchy,
84 * this can result in a deadlock situation (see comments in code below).
86 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
87 * on whether the name is to be looked up, created, renamed, or deleted.
88 * When CREATE, RENAME, or DELETE is specified, information usable in
89 * creating, renaming, or deleting a directory entry may be calculated.
90 * If flag has LOCKPARENT or'ed into it and the target of the pathname
91 * exists, lookup returns both the target and its parent directory locked.
92 * When creating or renaming and LOCKPARENT is specified, the target may
93 * not be ".". When deleting and LOCKPARENT is specified, the target may
94 * be "."., but the caller must check to ensure it does an vrele and vput
95 * instead of two vputs.
97 * Overall outline of ufs_lookup:
99 * search for name in directory, to found or notfound
101 * if creating, return locked directory, leaving info on available slots
104 * if at end of path and deleting, return information to allow delete
105 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
106 * inode and return info to allow rewrite
107 * if not at end, add name to cache; if at end and neither creating
108 * nor deleting, add name to cache
110 * ufs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
111 * struct componentname *a_cnp)
114 ufs_lookup(struct vop_old_lookup_args
*ap
)
116 struct vnode
*vdp
; /* vnode for directory being searched */
117 struct inode
*dp
; /* inode for directory being searched */
118 struct buf
*bp
; /* a buffer of directory entries */
119 struct direct
*ep
; /* the current directory entry */
120 int entryoffsetinblock
; /* offset of ep in bp's buffer */
121 enum {NONE
, COMPACT
, FOUND
} slotstatus
;
122 doff_t slotoffset
; /* offset of area with free space */
123 int slotsize
; /* size of area at slotoffset */
124 int slotfreespace
; /* amount of space free in slot */
125 int slotneeded
; /* size of the entry we're seeking */
126 int numdirpasses
; /* strategy for directory search */
127 doff_t endsearch
; /* offset to end directory search */
128 doff_t prevoff
; /* prev entry dp->i_offset */
129 struct vnode
*pdp
; /* saved dp during symlink work */
130 struct vnode
*tdp
; /* returned by VFS_VGET */
131 doff_t enduseful
; /* pointer past last used dir slot */
132 u_long bmask
; /* block offset mask */
133 int lockparent
; /* 1 => lockparent flag is set */
134 int wantparent
; /* 1 => wantparent or lockparent flag */
136 struct vnode
**vpp
= ap
->a_vpp
;
137 struct componentname
*cnp
= ap
->a_cnp
;
138 struct ucred
*cred
= cnp
->cn_cred
;
139 int flags
= cnp
->cn_flags
;
140 int nameiop
= cnp
->cn_nameiop
;
141 globaldata_t gd
= mycpu
;
145 cnp
->cn_flags
&= ~CNP_PDIRUNLOCK
;
147 * XXX there was a soft-update diff about this I couldn't merge.
148 * I think this was the equiv.
154 lockparent
= flags
& CNP_LOCKPARENT
;
155 wantparent
= flags
& (CNP_LOCKPARENT
|CNP_WANTPARENT
);
158 * We now have a segment name to search for, and a directory to search.
160 * Suppress search for slots unless creating
161 * file and at end of pathname, in which case
162 * we watch for a place to put the new file in
163 * case it doesn't already exist.
166 slotfreespace
= slotsize
= slotneeded
= 0;
167 if (nameiop
== NAMEI_CREATE
|| nameiop
== NAMEI_RENAME
) {
169 slotneeded
= DIRECTSIZ(cnp
->cn_namelen
);
171 bmask
= VFSTOUFS(vdp
->v_mount
)->um_mountp
->mnt_stat
.f_iosize
- 1;
175 * Use dirhash for fast operations on large directories. The logic
176 * to determine whether to hash the directory is contained within
177 * ufsdirhash_build(); a zero return means that it decided to hash
178 * this directory and it successfully built up the hash table.
180 if (ufsdirhash_build(dp
) == 0) {
181 /* Look for a free slot if needed. */
182 enduseful
= dp
->i_size
;
183 if (slotstatus
!= FOUND
) {
184 slotoffset
= ufsdirhash_findfree(dp
, slotneeded
,
186 if (slotoffset
>= 0) {
187 slotstatus
= COMPACT
;
188 enduseful
= ufsdirhash_enduseful(dp
);
190 enduseful
= dp
->i_size
;
193 /* Look up the component. */
195 entryoffsetinblock
= 0; /* silence compiler warning */
196 switch (ufsdirhash_lookup(dp
, cnp
->cn_nameptr
, cnp
->cn_namelen
,
197 &dp
->i_offset
, &bp
, nameiop
== NAMEI_DELETE
? &prevoff
: NULL
)) {
199 ep
= (struct direct
*)((char *)bp
->b_data
+
200 (dp
->i_offset
& bmask
));
203 dp
->i_offset
= roundup2(dp
->i_size
, DIRBLKSIZ
);
206 /* Something failed; just do a linear search. */
210 #endif /* UFS_DIRHASH */
212 * If there is cached information on a previous search of
213 * this directory, pick up where we last left off.
214 * We cache only lookups as these are the most common
215 * and have the greatest payoff. Caching CREATE has little
216 * benefit as it usually must search the entire directory
217 * to determine that the entry does not exist. Caching the
218 * location of the last DELETE or RENAME has not reduced
219 * profiling time and hence has been removed in the interest
222 if (nameiop
!= NAMEI_LOOKUP
|| dp
->i_diroff
== 0 ||
223 dp
->i_diroff
>= dp
->i_size
) {
224 entryoffsetinblock
= 0;
228 dp
->i_offset
= dp
->i_diroff
;
229 if ((entryoffsetinblock
= dp
->i_offset
& bmask
) &&
230 (error
= ffs_blkatoff(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
)))
233 gd
->gd_nchstats
->ncs_2passes
++;
235 prevoff
= dp
->i_offset
;
236 endsearch
= roundup2(dp
->i_size
, DIRBLKSIZ
);
240 while (dp
->i_offset
< endsearch
) {
242 * If necessary, get the next directory block.
244 if ((dp
->i_offset
& bmask
) == 0) {
248 ffs_blkatoff(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
);
251 entryoffsetinblock
= 0;
254 * If still looking for a slot, and at a DIRBLKSIZE
255 * boundary, have to start looking for free space again.
257 if (slotstatus
== NONE
&&
258 (entryoffsetinblock
& (DIRBLKSIZ
- 1)) == 0) {
263 * Get pointer to next entry.
264 * Full validation checks are slow, so we only check
265 * enough to insure forward progress through the
266 * directory. Complete checks can be run by patching
267 * "dirchk" to be true.
269 ep
= (struct direct
*)((char *)bp
->b_data
+ entryoffsetinblock
);
270 if (ep
->d_reclen
== 0 || ep
->d_reclen
>
271 DIRBLKSIZ
- (entryoffsetinblock
& (DIRBLKSIZ
- 1)) ||
272 (dirchk
&& ufs_dirbadentry(vdp
, ep
, entryoffsetinblock
))) {
275 ufs_dirbad(dp
, dp
->i_offset
, "mangled entry");
276 i
= DIRBLKSIZ
- (entryoffsetinblock
& (DIRBLKSIZ
- 1));
278 entryoffsetinblock
+= i
;
283 * If an appropriate sized slot has not yet been found,
284 * check to see if one is available. Also accumulate space
285 * in the current block so that we can determine if
286 * compaction is viable.
288 if (slotstatus
!= FOUND
) {
289 int size
= ep
->d_reclen
;
292 size
-= DIRSIZ(OFSFMT(vdp
), ep
);
294 if (size
>= slotneeded
) {
296 slotoffset
= dp
->i_offset
;
297 slotsize
= ep
->d_reclen
;
298 } else if (slotstatus
== NONE
) {
299 slotfreespace
+= size
;
300 if (slotoffset
== -1)
301 slotoffset
= dp
->i_offset
;
302 if (slotfreespace
>= slotneeded
) {
303 slotstatus
= COMPACT
;
304 slotsize
= dp
->i_offset
+
305 ep
->d_reclen
- slotoffset
;
312 * Check for a name match.
315 # if (BYTE_ORDER == LITTLE_ENDIAN)
319 namlen
= ep
->d_namlen
;
321 namlen
= ep
->d_namlen
;
323 if (namlen
== cnp
->cn_namelen
&&
324 (cnp
->cn_nameptr
[0] == ep
->d_name
[0]) &&
325 !bcmp(cnp
->cn_nameptr
, ep
->d_name
,
331 * Save directory entry's inode number and
332 * reclen in ndp->ni_ufs area, and release
335 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0 &&
336 ep
->d_type
== DT_WHT
) {
338 slotoffset
= dp
->i_offset
;
339 slotsize
= ep
->d_reclen
;
340 dp
->i_reclen
= slotsize
;
341 enduseful
= dp
->i_size
;
342 ap
->a_cnp
->cn_flags
|= CNP_ISWHITEOUT
;
346 dp
->i_ino
= ep
->d_ino
;
347 dp
->i_reclen
= ep
->d_reclen
;
351 prevoff
= dp
->i_offset
;
352 dp
->i_offset
+= ep
->d_reclen
;
353 entryoffsetinblock
+= ep
->d_reclen
;
355 enduseful
= dp
->i_offset
;
359 * If we started in the middle of the directory and failed
360 * to find our target, we must check the beginning as well.
362 if (numdirpasses
== 2) {
365 endsearch
= dp
->i_diroff
;
371 * If creating, and at end of pathname and current
372 * directory has not been removed, then can consider
373 * allowing file to be created.
375 if ((nameiop
== NAMEI_CREATE
|| nameiop
== NAMEI_RENAME
||
376 (nameiop
== NAMEI_DELETE
&&
377 (ap
->a_cnp
->cn_flags
& CNP_DOWHITEOUT
) &&
378 (ap
->a_cnp
->cn_flags
& CNP_ISWHITEOUT
))) &&
379 dp
->i_effnlink
!= 0) {
381 * Access for write is interpreted as allowing
382 * creation of files in the directory.
384 error
= VOP_ACCESS(vdp
, VWRITE
, cred
);
388 * Return an indication of where the new directory
389 * entry should be put. If we didn't find a slot,
390 * then set dp->i_count to 0 indicating
391 * that the new slot belongs at the end of the
392 * directory. If we found a slot, then the new entry
393 * can be put in the range from dp->i_offset to
394 * dp->i_offset + dp->i_count.
396 if (slotstatus
== NONE
) {
397 dp
->i_offset
= roundup2(dp
->i_size
, DIRBLKSIZ
);
399 enduseful
= dp
->i_offset
;
400 } else if (nameiop
== NAMEI_DELETE
) {
401 dp
->i_offset
= slotoffset
;
402 if ((dp
->i_offset
& (DIRBLKSIZ
- 1)) == 0)
405 dp
->i_count
= dp
->i_offset
- prevoff
;
407 dp
->i_offset
= slotoffset
;
408 dp
->i_count
= slotsize
;
409 if (enduseful
< slotoffset
+ slotsize
)
410 enduseful
= slotoffset
+ slotsize
;
412 dp
->i_endoff
= roundup2(enduseful
, DIRBLKSIZ
);
413 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
415 * We return with the directory locked, so that
416 * the parameters we set up above will still be
417 * valid if we actually decide to do a direnter().
418 * We return ni_vp == NULL to indicate that the entry
419 * does not currently exist; we leave a pointer to
420 * the (locked) directory inode in ndp->ni_dvp.
421 * The pathname buffer is saved so that the name
422 * can be obtained later.
424 * NB - if the directory is unlocked, then this
425 * information cannot be used.
429 cnp
->cn_flags
|= CNP_PDIRUNLOCK
;
431 return (EJUSTRETURN
);
436 if (numdirpasses
== 2)
437 gd
->gd_nchstats
->ncs_pass2
++;
439 * Check that directory length properly reflects presence
442 if (dp
->i_offset
+ DIRSIZ(OFSFMT(vdp
), ep
) > dp
->i_size
) {
443 ufs_dirbad(dp
, dp
->i_offset
, "i_size too small");
444 dp
->i_size
= dp
->i_offset
+ DIRSIZ(OFSFMT(vdp
), ep
);
445 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
450 * Found component in pathname.
451 * If the final component of path name, save information
452 * in the cache as to where the entry was found.
454 if (nameiop
== NAMEI_LOOKUP
)
455 dp
->i_diroff
= dp
->i_offset
&~ (DIRBLKSIZ
- 1);
458 * If deleting, and at end of pathname, return
459 * parameters which can be used to remove file.
460 * If the wantparent flag isn't set, we return only
461 * the directory (in ndp->ni_dvp), otherwise we go
462 * on and lock the inode, being careful with ".".
464 if (nameiop
== NAMEI_DELETE
) {
466 * Write access to directory required to delete files.
468 error
= VOP_ACCESS(vdp
, VWRITE
, cred
);
472 * Return pointer to current entry in dp->i_offset,
473 * and distance past previous entry (if there
474 * is a previous entry in this block) in dp->i_count.
475 * Save directory inode pointer in ndp->ni_dvp for dirremove().
477 if ((dp
->i_offset
& (DIRBLKSIZ
- 1)) == 0)
480 dp
->i_count
= dp
->i_offset
- prevoff
;
481 if (dp
->i_number
== dp
->i_ino
) {
486 if (flags
& CNP_ISDOTDOT
)
487 vn_unlock(vdp
); /* race to get the inode */
488 error
= VFS_VGET(vdp
->v_mount
, dp
->i_ino
, &tdp
);
489 if (flags
& CNP_ISDOTDOT
) {
490 if (vn_lock(vdp
, LK_EXCLUSIVE
| LK_RETRY
) != 0)
491 cnp
->cn_flags
|= CNP_PDIRUNLOCK
;
496 * If directory is "sticky", then user must own
497 * the directory, or the file in it, else she
498 * may not delete it (unless she's root). This
499 * implements append-only directories.
501 if ((dp
->i_mode
& ISVTX
) &&
503 cred
->cr_uid
!= dp
->i_uid
&&
504 VTOI(tdp
)->i_uid
!= cred
->cr_uid
) {
511 cnp
->cn_flags
|= CNP_PDIRUNLOCK
;
517 * If rewriting (RENAME), return the inode and the
518 * information required to rewrite the present directory
519 * Must get inode of directory entry to verify it's a
520 * regular file, or empty directory.
522 if (nameiop
== NAMEI_RENAME
&& wantparent
) {
523 if ((error
= VOP_ACCESS(vdp
, VWRITE
, cred
)) != 0)
526 * Careful about locking second inode.
527 * This can only occur if the target is ".".
529 if (dp
->i_number
== dp
->i_ino
)
531 if (flags
& CNP_ISDOTDOT
)
532 vn_unlock(vdp
); /* race to get the inode */
533 error
= VFS_VGET(vdp
->v_mount
, dp
->i_ino
, &tdp
);
534 if (flags
& CNP_ISDOTDOT
) {
535 if (vn_lock(vdp
, LK_EXCLUSIVE
| LK_RETRY
) != 0)
536 cnp
->cn_flags
|= CNP_PDIRUNLOCK
;
543 cnp
->cn_flags
|= CNP_PDIRUNLOCK
;
549 * Step through the translation in the name. We do not `vput' the
550 * directory because we may need it again if a symbolic link
551 * is relative to the current directory. Instead we save it
552 * unlocked as "pdp". We must get the target inode before unlocking
553 * the directory to insure that the inode will not be removed
554 * before we get it. We prevent deadlock by always fetching
555 * inodes from the root, moving down the directory tree. Thus
556 * when following backward pointers ".." we must unlock the
557 * parent directory before getting the requested directory.
558 * There is a potential race condition here if both the current
559 * and parent directories are removed before the VFS_VGET for the
560 * inode associated with ".." returns. We hope that this occurs
561 * infrequently since we cannot avoid this race condition without
562 * implementing a sophisticated deadlock detection algorithm.
563 * Note also that this simple deadlock detection scheme will not
564 * work if the filesystem has any hard links other than ".."
565 * that point backwards in the directory structure.
568 if (flags
& CNP_ISDOTDOT
) {
569 vn_unlock(pdp
); /* race to get the inode */
570 cnp
->cn_flags
|= CNP_PDIRUNLOCK
;
571 if ((error
= VFS_VGET(vdp
->v_mount
, dp
->i_ino
, &tdp
)) != 0) {
572 if (vn_lock(pdp
, LK_EXCLUSIVE
| LK_RETRY
) == 0)
573 cnp
->cn_flags
&= ~CNP_PDIRUNLOCK
;
577 if ((error
= vn_lock(pdp
, LK_EXCLUSIVE
)) != 0) {
581 cnp
->cn_flags
&= ~CNP_PDIRUNLOCK
;
584 } else if (dp
->i_number
== dp
->i_ino
) {
585 vref(vdp
); /* we want ourself, ie "." */
588 error
= VFS_VGET(vdp
->v_mount
, dp
->i_ino
, &tdp
);
593 cnp
->cn_flags
|= CNP_PDIRUNLOCK
;
601 ufs_dirbad(struct inode
*ip
, doff_t offset
, char *how
)
605 mp
= ITOV(ip
)->v_mount
;
606 (void)kprintf("%s: bad dir ino %lu at offset %ld: %s\n",
607 mp
->mnt_stat
.f_mntfromname
, (u_long
)ip
->i_number
, (long)offset
, how
);
608 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0)
609 panic("ufs_dirbad: bad dir");
613 * Do consistency checking on a directory entry:
614 * record length must be multiple of 4
615 * entry must fit in rest of its DIRBLKSIZ block
616 * record must be large enough to contain entry
617 * name is not longer than MAXNAMLEN
618 * name must be as long as advertised, and null terminated
621 ufs_dirbadentry(struct vnode
*dp
, struct direct
*ep
, int entryoffsetinblock
)
626 # if (BYTE_ORDER == LITTLE_ENDIAN)
630 namlen
= ep
->d_namlen
;
632 namlen
= ep
->d_namlen
;
634 if ((ep
->d_reclen
& 0x3) != 0 ||
635 ep
->d_reclen
> DIRBLKSIZ
- (entryoffsetinblock
& (DIRBLKSIZ
- 1)) ||
636 ep
->d_reclen
< DIRSIZ(OFSFMT(dp
), ep
) || namlen
> MAXNAMLEN
) {
638 kprintf("First bad\n");
643 for (i
= 0; i
< namlen
; i
++)
644 if (ep
->d_name
[i
] == '\0') {
646 kprintf("Second bad\n");
657 * Construct a new directory entry after a call to namei, using the
658 * parameters that it left in the componentname argument cnp. The
659 * argument ip is the inode to which the new directory entry will refer.
662 ufs_makedirentry(struct inode
*ip
, struct componentname
*cnp
,
663 struct direct
*newdirp
)
666 newdirp
->d_ino
= ip
->i_number
;
667 newdirp
->d_namlen
= cnp
->cn_namelen
;
668 bcopy(cnp
->cn_nameptr
, newdirp
->d_name
, (unsigned)cnp
->cn_namelen
+ 1);
669 if (ITOV(ip
)->v_mount
->mnt_maxsymlinklen
> 0)
670 newdirp
->d_type
= IFTODT(ip
->i_mode
);
673 # if (BYTE_ORDER == LITTLE_ENDIAN)
674 { u_char tmp
= newdirp
->d_namlen
;
675 newdirp
->d_namlen
= newdirp
->d_type
;
676 newdirp
->d_type
= tmp
; }
682 * Write a directory entry after a call to namei, using the parameters
683 * that it left in the directory inode. The argument dirp is the new directory
684 * entry contents. Dvp is a pointer to the directory to be written,
685 * which was left locked by namei. Remaining parameters (dp->i_offset,
686 * dp->i_count) indicate how the space for the new entry is to be obtained.
687 * Non-null bp indicates that a directory is being created (for the
688 * soft dependency code).
691 ufs_direnter(struct vnode
*dvp
, struct vnode
*tvp
, struct direct
*dirp
,
692 struct componentname
*cnp
, struct buf
*newdirbp
)
695 struct thread
*td
= curthread
; /* XXX */
700 struct direct
*ep
, *nep
;
701 int error
, ret
, blkoff
, loc
, spacefree
, flags
;
704 KKASSERT(td
->td_proc
); /* YYY use/require cred passed in cnp? */
705 cred
= td
->td_proc
->p_ucred
;
708 newentrysize
= DIRSIZ(OFSFMT(dvp
), dirp
);
710 if (dp
->i_count
== 0) {
712 * If dp->i_count is 0, then namei could find no
713 * space in the directory. Here, dp->i_offset will
714 * be on a directory block boundary and we will write the
715 * new entry into a fresh block.
717 if (dp
->i_offset
& (DIRBLKSIZ
- 1))
718 panic("ufs_direnter: newblk");
719 vnode_pager_setsize(dvp
, dp
->i_offset
+ DIRBLKSIZ
);
721 if (!DOINGSOFTDEP(dvp
) && !DOINGASYNC(dvp
))
723 if ((error
= VOP_BALLOC(dvp
, (off_t
)dp
->i_offset
, DIRBLKSIZ
,
724 cred
, flags
, &bp
)) != 0) {
725 if (DOINGSOFTDEP(dvp
) && newdirbp
!= NULL
)
729 dp
->i_size
= dp
->i_offset
+ DIRBLKSIZ
;
730 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
731 dirp
->d_reclen
= DIRBLKSIZ
;
732 blkoff
= dp
->i_offset
&
733 (VFSTOUFS(dvp
->v_mount
)->um_mountp
->mnt_stat
.f_iosize
- 1);
734 bcopy((caddr_t
)dirp
, (caddr_t
)bp
->b_data
+ blkoff
,newentrysize
);
736 if (dp
->i_dirhash
!= NULL
) {
737 ufsdirhash_newblk(dp
, dp
->i_offset
);
738 ufsdirhash_add(dp
, dirp
, dp
->i_offset
);
739 ufsdirhash_checkblock(dp
, (char *)bp
->b_data
+ blkoff
,
743 if (DOINGSOFTDEP(dvp
)) {
745 * Ensure that the entire newly allocated block is a
746 * valid directory so that future growth within the
747 * block does not have to ensure that the block is
748 * written before the inode.
751 while (blkoff
< bp
->b_bcount
) {
753 (bp
->b_data
+ blkoff
))->d_reclen
= DIRBLKSIZ
;
756 softdep_setup_directory_add(bp
, dp
, dp
->i_offset
,
757 dirp
->d_ino
, newdirbp
);
759 return (ffs_update(dvp
, 0));
761 if (DOINGASYNC(dvp
)) {
763 return (ffs_update(dvp
, 0));
766 ret
= ffs_update(dvp
, 1);
773 * If dp->i_count is non-zero, then namei found space for the new
774 * entry in the range dp->i_offset to dp->i_offset + dp->i_count
775 * in the directory. To use this space, we may have to compact
776 * the entries located there, by copying them together towards the
777 * beginning of the block, leaving the free space in one usable
782 * Increase size of directory if entry eats into new space.
783 * This should never push the size past a new multiple of
786 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
788 if (dp
->i_offset
+ dp
->i_count
> dp
->i_size
)
789 dp
->i_size
= dp
->i_offset
+ dp
->i_count
;
791 * Get the block containing the space for the new directory entry.
793 error
= ffs_blkatoff(dvp
, (off_t
)dp
->i_offset
, &dirbuf
, &bp
);
795 if (DOINGSOFTDEP(dvp
) && newdirbp
!= NULL
)
800 * Find space for the new entry. In the simple case, the entry at
801 * offset base will have the space. If it does not, then namei
802 * arranged that compacting the region dp->i_offset to
803 * dp->i_offset + dp->i_count would yield the space.
805 ep
= (struct direct
*)dirbuf
;
806 dsize
= ep
->d_ino
? DIRSIZ(OFSFMT(dvp
), ep
) : 0;
807 spacefree
= ep
->d_reclen
- dsize
;
808 for (loc
= ep
->d_reclen
; loc
< dp
->i_count
; ) {
809 nep
= (struct direct
*)(dirbuf
+ loc
);
811 /* Trim the existing slot (NB: dsize may be zero). */
812 ep
->d_reclen
= dsize
;
813 ep
= (struct direct
*)((char *)ep
+ dsize
);
815 /* Read nep->d_reclen now as the bcopy() may clobber it. */
816 loc
+= nep
->d_reclen
;
817 if (nep
->d_ino
== 0) {
819 * A mid-block unused entry. Such entries are
820 * never created by the kernel, but fsck_ffs
821 * can create them (and it doesn't fix them).
823 * Add up the free space, and initialise the
824 * relocated entry since we don't bcopy it.
826 spacefree
+= nep
->d_reclen
;
831 dsize
= DIRSIZ(OFSFMT(dvp
), nep
);
832 spacefree
+= nep
->d_reclen
- dsize
;
834 if (dp
->i_dirhash
!= NULL
)
835 ufsdirhash_move(dp
, nep
,
836 dp
->i_offset
+ ((char *)nep
- dirbuf
),
837 dp
->i_offset
+ ((char *)ep
- dirbuf
));
839 if (DOINGSOFTDEP(dvp
))
840 softdep_change_directoryentry_offset(dp
, dirbuf
,
841 (caddr_t
)nep
, (caddr_t
)ep
, dsize
);
843 bcopy((caddr_t
)nep
, (caddr_t
)ep
, dsize
);
846 * Here, `ep' points to a directory entry containing `dsize' in-use
847 * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
848 * then the entry is completely unused (dsize == 0). The value
849 * of ep->d_reclen is always indeterminate.
851 * Update the pointer fields in the previous entry (if any),
852 * copy in the new entry, and write out the block.
854 if (ep
->d_ino
== 0 ||
855 (ep
->d_ino
== WINO
&&
856 bcmp(ep
->d_name
, dirp
->d_name
, dirp
->d_namlen
) == 0)) {
857 if (spacefree
+ dsize
< newentrysize
)
858 panic("ufs_direnter: compact1");
859 dirp
->d_reclen
= spacefree
+ dsize
;
861 if (spacefree
< newentrysize
)
862 panic("ufs_direnter: compact2");
863 dirp
->d_reclen
= spacefree
;
864 ep
->d_reclen
= dsize
;
865 ep
= (struct direct
*)((char *)ep
+ dsize
);
868 if (dp
->i_dirhash
!= NULL
&& (ep
->d_ino
== 0 ||
869 dirp
->d_reclen
== spacefree
))
870 ufsdirhash_add(dp
, dirp
, dp
->i_offset
+ ((char *)ep
- dirbuf
));
872 bcopy((caddr_t
)dirp
, (caddr_t
)ep
, (uint
)newentrysize
);
874 if (dp
->i_dirhash
!= NULL
)
875 ufsdirhash_checkblock(dp
, dirbuf
-
876 (dp
->i_offset
& (DIRBLKSIZ
- 1)),
877 dp
->i_offset
& ~(DIRBLKSIZ
- 1));
880 if (DOINGSOFTDEP(dvp
)) {
881 softdep_setup_directory_add(bp
, dp
,
882 dp
->i_offset
+ (caddr_t
)ep
- dirbuf
, dirp
->d_ino
, newdirbp
);
885 if (DOINGASYNC(dvp
)) {
892 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
894 * If all went well, and the directory can be shortened, proceed
895 * with the truncation. Note that we have to unlock the inode for
896 * the entry that we just entered, as the truncation may need to
897 * lock other inodes which can lead to deadlock if we also hold a
898 * lock on the newly entered node.
900 if (error
== 0 && dp
->i_endoff
&& dp
->i_endoff
< dp
->i_size
) {
904 if (dp
->i_dirhash
!= NULL
)
905 ufsdirhash_dirtrunc(dp
, dp
->i_endoff
);
907 (void)ffs_truncate(dvp
, (off_t
)dp
->i_endoff
, IO_SYNC
, cred
);
909 vn_lock(tvp
, LK_EXCLUSIVE
| LK_RETRY
);
915 * Remove a directory entry after a call to namei, using
916 * the parameters which it left in the directory inode. The entry
917 * dp->i_offset contains the offset into the directory of the
918 * entry to be eliminated. The dp->i_count field contains the
919 * size of the previous record in the directory. If this
920 * is 0, the first entry is being deleted, so we need only
921 * zero the inode number to mark the entry as free. If the
922 * entry is not the first in the directory, we must reclaim
923 * the space of the now empty record by adding the record size
924 * to the size of the previous entry.
927 ufs_dirremove(struct vnode
*dvp
, struct inode
*ip
, int flags
, int isrmdir
)
936 if (flags
& CNP_DOWHITEOUT
) {
938 * Whiteout entry: set d_ino to WINO.
941 ffs_blkatoff(dvp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
)) != 0)
948 if ((error
= ffs_blkatoff(dvp
,
949 (off_t
)(dp
->i_offset
- dp
->i_count
), (char **)&ep
, &bp
)) != 0)
953 * Remove the dirhash entry. This is complicated by the fact
954 * that `ep' is the previous entry when dp->i_count != 0.
956 if (dp
->i_dirhash
!= NULL
)
957 ufsdirhash_remove(dp
, (dp
->i_count
== 0) ? ep
:
958 (struct direct
*)((char *)ep
+ ep
->d_reclen
), dp
->i_offset
);
960 if (dp
->i_count
== 0) {
962 * First entry in block: set d_ino to zero.
967 * Collapse new free space into previous entry.
969 ep
->d_reclen
+= dp
->i_reclen
;
972 if (dp
->i_dirhash
!= NULL
)
973 ufsdirhash_checkblock(dp
, (char *)ep
-
974 ((dp
->i_offset
- dp
->i_count
) & (DIRBLKSIZ
- 1)),
975 dp
->i_offset
& ~(DIRBLKSIZ
- 1));
978 if (DOINGSOFTDEP(dvp
)) {
981 softdep_change_linkcnt(ip
);
982 softdep_setup_remove(bp
, dp
, ip
, isrmdir
);
984 if (softdep_slowdown(dvp
)) {
994 ip
->i_flag
|= IN_CHANGE
;
996 if (flags
& CNP_DOWHITEOUT
)
998 else if (DOINGASYNC(dvp
) && dp
->i_count
!= 0) {
1002 error
= bowrite(bp
);
1004 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1009 * Rewrite an existing directory entry to point at the inode
1010 * supplied. The parameters describing the directory entry are
1011 * set up by a call to namei.
1014 ufs_dirrewrite(struct inode
*dp
, struct inode
*oip
, ino_t newinum
, int newtype
,
1019 struct vnode
*vdp
= ITOV(dp
);
1022 error
= ffs_blkatoff(vdp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
);
1025 ep
->d_ino
= newinum
;
1027 ep
->d_type
= newtype
;
1029 if (DOINGSOFTDEP(vdp
)) {
1030 softdep_change_linkcnt(oip
);
1031 softdep_setup_directory_change(bp
, dp
, oip
, newinum
, isrmdir
);
1035 oip
->i_flag
|= IN_CHANGE
;
1036 if (DOINGASYNC(vdp
)) {
1040 error
= bowrite(bp
);
1043 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1048 * Check if a directory is empty or not.
1049 * Inode supplied must be locked.
1051 * Using a struct dirtemplate here is not precisely
1052 * what we want, but better than using a struct direct.
1054 * NB: does not handle corrupted directories.
1057 ufs_dirempty(struct inode
*ip
, ino_t parentino
, struct ucred
*cred
)
1060 struct dirtemplate dbuf
;
1061 struct direct
*dp
= (struct direct
*)&dbuf
;
1062 int error
, count
, namlen
;
1063 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1065 for (off
= 0; off
< ip
->i_size
; off
+= dp
->d_reclen
) {
1066 error
= vn_rdwr(UIO_READ
, ITOV(ip
), (caddr_t
)dp
, MINDIRSIZ
, off
,
1067 UIO_SYSSPACE
, IO_NODELOCKED
, cred
, &count
);
1069 * Since we read MINDIRSIZ, residual must
1070 * be 0 unless we're at end of file.
1072 if (error
|| count
!= 0)
1074 /* avoid infinite loops */
1075 if (dp
->d_reclen
== 0)
1077 /* skip empty entries */
1078 if (dp
->d_ino
== 0 || dp
->d_ino
== WINO
)
1080 /* accept only "." and ".." */
1081 # if (BYTE_ORDER == LITTLE_ENDIAN)
1082 if (OFSFMT(ITOV(ip
)))
1083 namlen
= dp
->d_type
;
1085 namlen
= dp
->d_namlen
;
1087 namlen
= dp
->d_namlen
;
1091 if (dp
->d_name
[0] != '.')
1094 * At this point namlen must be 1 or 2.
1095 * 1 implies ".", 2 implies ".." if second
1098 if (namlen
== 1 && dp
->d_ino
== ip
->i_number
)
1100 if (dp
->d_name
[1] == '.' && dp
->d_ino
== parentino
)
1108 * Check if source directory is in the path of the target directory.
1109 * Target is supplied locked, source is unlocked.
1110 * The target is always vput before returning.
1113 ufs_checkpath(struct inode
*source
, struct inode
*target
, struct ucred
*cred
)
1116 int error
, rootino
, namlen
;
1117 struct dirtemplate dirbuf
;
1120 if (target
->i_number
== source
->i_number
) {
1126 if (target
->i_number
== rootino
)
1130 if (vp
->v_type
!= VDIR
) {
1134 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&dirbuf
,
1135 sizeof (struct dirtemplate
), (off_t
)0,
1136 UIO_SYSSPACE
, IO_NODELOCKED
, cred
, (int *)0);
1139 # if (BYTE_ORDER == LITTLE_ENDIAN)
1141 namlen
= dirbuf
.dotdot_type
;
1143 namlen
= dirbuf
.dotdot_namlen
;
1145 namlen
= dirbuf
.dotdot_namlen
;
1148 dirbuf
.dotdot_name
[0] != '.' ||
1149 dirbuf
.dotdot_name
[1] != '.') {
1153 if (dirbuf
.dotdot_ino
== source
->i_number
) {
1157 if (dirbuf
.dotdot_ino
== rootino
)
1160 error
= VFS_VGET(vp
->v_mount
, dirbuf
.dotdot_ino
, &vp
);
1168 if (error
== ENOTDIR
)
1169 kprintf("checkpath: .. not a directory\n");