]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_lookup.c
48bbde8c5ec7b83091ae7c1f3fc9139737eff840
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
62 #include <rev_endian_fs.h>
63 #include <sys/param.h>
64 #include <sys/namei.h>
67 #include <sys/mount_internal.h>
68 #include <sys/vnode_internal.h>
69 #include <sys/quota.h>
70 #include <sys/kauth.h>
71 #include <sys/uio_internal.h>
73 #include <ufs/ufs/quota.h>
74 #include <ufs/ufs/inode.h>
75 #include <ufs/ufs/dir.h>
76 #include <ufs/ufs/ufsmount.h>
77 #include <ufs/ufs/ufs_extern.h>
78 #include <ufs/ffs/ffs_extern.h>
80 #include <ufs/ufs/ufs_byte_order.h>
81 #include <architecture/byte_order.h>
82 #endif /* REV_ENDIAN_FS */
84 extern struct nchstats nchstats
;
91 #define FSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
94 * Convert a component of a pathname into a pointer to a locked inode.
95 * This is a very central and rather complicated routine.
96 * If the file system is not maintained in a strict tree hierarchy,
97 * this can result in a deadlock situation (see comments in code below).
99 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
100 * on whether the name is to be looked up, created, renamed, or deleted.
101 * When CREATE, RENAME, or DELETE is specified, information usable in
102 * creating, renaming, or deleting a directory entry may be calculated.
103 * If flag has LOCKPARENT or'ed into it and the target of the pathname
104 * exists, lookup returns both the target and its parent directory locked.
105 * When creating or renaming and LOCKPARENT is specified, the target may
106 * not be ".". When deleting and LOCKPARENT is specified, the target may
109 * Overall outline of ufs_lookup:
111 * check accessibility of directory
112 * look for name in cache, if found, then if at end of path
113 * and deleting or creating, drop it, else return name
114 * search for name in directory, to found or notfound
116 * if creating, return locked directory, leaving info on available slots
119 * if at end of path and deleting, return information to allow delete
120 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
121 * inode and return info to allow rewrite
122 * if not at end, add name to cache; if at end and neither creating
123 * nor deleting, add name to cache
127 struct vnop_lookup_args
/* {
129 struct vnode **a_vpp;
130 struct componentname *a_cnp;
131 vfs_context_t a_context
134 register struct vnode
*vdp
; /* vnode for directory being searched */
135 register struct inode
*dp
; /* inode for directory being searched */
136 struct buf
*bp
; /* a buffer of directory entries */
137 register struct direct
*ep
; /* the current directory entry */
138 int entryoffsetinblock
; /* offset of ep in bp's buffer */
139 enum {NONE
, COMPACT
, FOUND
} slotstatus
;
140 doff_t slotoffset
; /* offset of area with free space */
141 int slotsize
; /* size of area at slotoffset */
142 int slotfreespace
; /* amount of space free in slot */
143 int slotneeded
; /* size of the entry we're seeking */
144 int numdirpasses
; /* strategy for directory search */
145 doff_t endsearch
; /* offset to end directory search */
146 doff_t prevoff
; /* prev entry dp->i_offset */
147 struct vnode
*pdp
; /* saved dp during symlink work */
148 struct vnode
*tdp
; /* returned by VFS_VGET */
149 doff_t enduseful
; /* pointer past last used dir slot */
150 u_long bmask
; /* block offset mask */
151 int wantparent
; /* 1 => wantparent or lockparent flag */
153 struct vnode
**vpp
= ap
->a_vpp
;
154 struct componentname
*cnp
= ap
->a_cnp
;
155 int flags
= cnp
->cn_flags
;
156 int nameiop
= cnp
->cn_nameiop
;
157 vfs_context_t context
= ap
->a_context
;
161 #endif /* REV_ENDIAN_FS */
164 cred
= vfs_context_ucred(context
);
171 wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
174 rev_endian
=(vdp
->v_mount
->mnt_flag
& MNT_REVEND
);
175 #endif /* REV_ENDIAN_FS */
178 * Check accessiblity of directory.
180 if ((dp
->i_mode
& IFMT
) != IFDIR
)
184 * We now have a segment name to search for, and a directory to search.
186 * Before tediously performing a linear scan of the directory,
187 * check the name cache to see if the directory/name pair
188 * we are looking for is known already.
190 if (error
= cache_lookup(vdp
, vpp
, cnp
)) {
196 * Suppress search for slots unless creating
197 * file and at end of pathname, in which case
198 * we watch for a place to put the new file in
199 * case it doesn't already exist.
202 slotfreespace
= slotsize
= slotneeded
= 0;
203 if ((nameiop
== CREATE
|| nameiop
== RENAME
) &&
204 (flags
& ISLASTCN
)) {
206 slotneeded
= (sizeof(struct direct
) - MAXNAMLEN
+
207 cnp
->cn_namelen
+ 3) &~ 3;
210 * If there is cached information on a previous search of
211 * this directory, pick up where we last left off.
212 * We cache only lookups as these are the most common
213 * and have the greatest payoff. Caching CREATE has little
214 * benefit as it usually must search the entire directory
215 * to determine that the entry does not exist. Caching the
216 * location of the last DELETE or RENAME has not reduced
217 * profiling time and hence has been removed in the interest
220 bmask
= VFSTOUFS(vdp
->v_mount
)->um_mountp
->mnt_vfsstat
.f_iosize
- 1;
221 if (nameiop
!= LOOKUP
|| dp
->i_diroff
== 0 ||
222 dp
->i_diroff
> dp
->i_size
) {
223 entryoffsetinblock
= 0;
227 dp
->i_offset
= dp
->i_diroff
;
228 if ((entryoffsetinblock
= dp
->i_offset
& bmask
) &&
229 (error
= ffs_blkatoff(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
)))
232 nchstats
.ncs_2passes
++;
234 prevoff
= dp
->i_offset
;
235 endsearch
= roundup(dp
->i_size
, DIRBLKSIZ
);
239 while (dp
->i_offset
< endsearch
) {
241 * If necessary, get the next directory block.
243 if ((dp
->i_offset
& bmask
) == 0) {
247 byte_swap_dir_block_out(bp
);
248 #endif /* REV_ENDIAN_FS */
251 if (error
= ffs_blkatoff(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
))
253 entryoffsetinblock
= 0;
256 * If still looking for a slot, and at a DIRBLKSIZE
257 * boundary, have to start looking for free space again.
259 if (slotstatus
== NONE
&&
260 (entryoffsetinblock
& (DIRBLKSIZ
- 1)) == 0) {
265 * Get pointer to next entry.
266 * Full validation checks are slow, so we only check
267 * enough to insure forward progress through the
268 * directory. Complete checks can be run by patching
269 * "dirchk" to be true.
271 ep
= (struct direct
*)((char *)buf_dataptr(bp
) + entryoffsetinblock
);
272 if (ep
->d_reclen
== 0 ||
273 dirchk
&& ufs_dirbadentry(vdp
, ep
, entryoffsetinblock
)) {
276 ufs_dirbad(dp
, dp
->i_offset
, "mangled entry");
277 i
= DIRBLKSIZ
- (entryoffsetinblock
& (DIRBLKSIZ
- 1));
279 entryoffsetinblock
+= i
;
284 * If an appropriate sized slot has not yet been found,
285 * check to see if one is available. Also accumulate space
286 * in the current block so that we can determine if
287 * compaction is viable.
289 if (slotstatus
!= FOUND
) {
290 int size
= ep
->d_reclen
;
293 size
-= DIRSIZ(FSFMT(vdp
), ep
);
295 if (size
>= slotneeded
) {
297 slotoffset
= dp
->i_offset
;
298 slotsize
= ep
->d_reclen
;
299 } else if (slotstatus
== NONE
) {
300 slotfreespace
+= size
;
301 if (slotoffset
== -1)
302 slotoffset
= dp
->i_offset
;
303 if (slotfreespace
>= slotneeded
) {
304 slotstatus
= COMPACT
;
305 slotsize
= dp
->i_offset
+
306 ep
->d_reclen
- slotoffset
;
313 * Check for a name match.
316 # if (BYTE_ORDER == LITTLE_ENDIAN)
317 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0)
318 namlen
= ep
->d_namlen
;
322 namlen
= ep
->d_namlen
;
324 if (namlen
== cnp
->cn_namelen
&&
325 !bcmp(cnp
->cn_nameptr
, ep
->d_name
,
328 * Save directory entry's inode number and
329 * reclen in ndp->ni_ufs area, and release
332 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0 &&
333 ep
->d_type
== DT_WHT
) {
335 slotoffset
= dp
->i_offset
;
336 slotsize
= ep
->d_reclen
;
337 dp
->i_reclen
= slotsize
;
338 enduseful
= dp
->i_size
;
339 ap
->a_cnp
->cn_flags
|= ISWHITEOUT
;
343 dp
->i_ino
= ep
->d_ino
;
344 dp
->i_reclen
= ep
->d_reclen
;
347 byte_swap_dir_block_out(bp
);
348 #endif /* REV_ENDIAN_FS */
353 prevoff
= dp
->i_offset
;
354 dp
->i_offset
+= ep
->d_reclen
;
355 entryoffsetinblock
+= ep
->d_reclen
;
357 enduseful
= dp
->i_offset
;
361 * If we started in the middle of the directory and failed
362 * to find our target, we must check the beginning as well.
364 if (numdirpasses
== 2) {
367 endsearch
= dp
->i_diroff
;
373 byte_swap_dir_block_out(bp
);
374 #endif /* REV_ENDIAN_FS */
378 * If creating, and at end of pathname and current
379 * directory has not been removed, then can consider
380 * allowing file to be created.
382 if ((nameiop
== CREATE
|| nameiop
== RENAME
||
383 (nameiop
== DELETE
&&
384 (ap
->a_cnp
->cn_flags
& DOWHITEOUT
) &&
385 (ap
->a_cnp
->cn_flags
& ISWHITEOUT
))) &&
386 (flags
& ISLASTCN
) && dp
->i_nlink
!= 0) {
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
= roundup(dp
->i_size
, DIRBLKSIZ
);
399 enduseful
= dp
->i_offset
;
400 } else if (nameiop
== 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
= roundup(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.
422 * NB - if the directory is unlocked, then this
423 * information cannot be used.
429 * Insert name into cache (as non-existent) if appropriate.
431 if ((cnp
->cn_flags
& MAKEENTRY
) && nameiop
!= CREATE
)
432 cache_enter(vdp
, *vpp
, cnp
);
437 if (numdirpasses
== 2)
438 nchstats
.ncs_pass2
++;
440 * Check that directory length properly reflects presence
443 if (entryoffsetinblock
+ DIRSIZ(FSFMT(vdp
), ep
) > dp
->i_size
) {
444 ufs_dirbad(dp
, dp
->i_offset
, "i_size too small");
445 dp
->i_size
= entryoffsetinblock
+ DIRSIZ(FSFMT(vdp
), ep
);
446 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 ((flags
& ISLASTCN
) && nameiop
== 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
== DELETE
&& (flags
& ISLASTCN
)) {
466 * Return pointer to current entry in dp->i_offset,
467 * and distance past previous entry (if there
468 * is a previous entry in this block) in dp->i_count.
469 * Save directory inode pointer in ndp->ni_dvp for dirremove().
471 if ((dp
->i_offset
& (DIRBLKSIZ
- 1)) == 0)
474 dp
->i_count
= dp
->i_offset
- prevoff
;
475 if (dp
->i_number
== dp
->i_ino
) {
481 if (error
= ffs_vget_internal(vdp
->v_mount
, dp
->i_ino
, &tdp
, vdp
, cnp
, 0, 0))
488 * If rewriting (RENAME), return the inode and the
489 * information required to rewrite the present directory
490 * Must get inode of directory entry to verify it's a
491 * regular file, or empty directory.
493 if (nameiop
== RENAME
&& wantparent
&& (flags
& ISLASTCN
)) {
495 * Careful about locking second inode.
496 * This can only occur if the target is ".".
498 if (dp
->i_number
== dp
->i_ino
) {
502 if (error
= ffs_vget_internal(vdp
->v_mount
, dp
->i_ino
, &tdp
, vdp
, cnp
, 0, 0))
510 * Step through the translation in the name. We do not `vnode_put' the
511 * directory because we may need it again if a symbolic link
512 * is relative to the current directory. Instead we save it
513 * unlocked as "pdp". We must get the target inode before unlocking
514 * the directory to insure that the inode will not be removed
515 * before we get it. We prevent deadlock by always fetching
516 * inodes from the root, moving down the directory tree. Thus
517 * when following backward pointers ".." we must unlock the
518 * parent directory before getting the requested directory.
519 * There is a potential race condition here if both the current
520 * and parent directories are removed before the VFS_VGET for the
521 * inode associated with ".." returns. We hope that this occurs
522 * infrequently since we cannot avoid this race condition without
523 * implementing a sophisticated deadlock detection algorithm.
524 * Note also that this simple deadlock detection scheme will not
525 * work if the file system has any hard links other than ".."
526 * that point backwards in the directory structure.
529 if (flags
& ISDOTDOT
) {
530 if (error
= ffs_vget_internal(vdp
->v_mount
, dp
->i_ino
, &tdp
, vdp
, cnp
, 0, 0)) {
534 } else if (dp
->i_number
== dp
->i_ino
) {
535 vnode_get(vdp
); /* we want ourself, ie "." */
538 if (error
= ffs_vget_internal(vdp
->v_mount
, dp
->i_ino
, &tdp
, vdp
, cnp
, 0, 0))
549 ufs_dirbad(ip
, offset
, how
)
556 mp
= ITOV(ip
)->v_mount
;
557 (void)printf("%s: bad dir ino %d at offset %d: %s\n",
558 mp
->mnt_vfsstat
.f_mntonname
, ip
->i_number
, offset
, how
);
559 if ((mp
->mnt_vfsstat
.f_flags
& MNT_RDONLY
) == 0)
564 * Do consistency checking on a directory entry:
565 * record length must be multiple of 4
566 * entry must fit in rest of its DIRBLKSIZ block
567 * record must be large enough to contain entry
568 * name is not longer than MAXNAMLEN
569 * name must be as long as advertised, and null terminated
572 ufs_dirbadentry(dp
, ep
, entryoffsetinblock
)
574 register struct direct
*ep
;
575 int entryoffsetinblock
;
580 # if (BYTE_ORDER == LITTLE_ENDIAN)
581 if (dp
->v_mount
->mnt_maxsymlinklen
> 0)
582 namlen
= ep
->d_namlen
;
586 namlen
= ep
->d_namlen
;
588 if ((ep
->d_reclen
& 0x3) != 0 ||
589 ep
->d_reclen
> DIRBLKSIZ
- (entryoffsetinblock
& (DIRBLKSIZ
- 1)) ||
590 ep
->d_reclen
< DIRSIZ(FSFMT(dp
), ep
) || namlen
> MAXNAMLEN
) {
592 printf("First bad\n");
597 for (i
= 0; i
< namlen
; i
++)
598 if (ep
->d_name
[i
] == '\0') {
600 printf("Second bad\n");
611 * Write a directory entry after a call to namei, using the parameters
612 * that it left in nameidata. The argument ip is the inode which the new
613 * directory entry will refer to. Dvp is a pointer to the directory to
614 * be written, which was left locked by namei. Remaining parameters
615 * (dp->i_offset, dp->i_count) indicate how the space for the new
616 * entry is to be obtained.
619 ufs_direnter(ip
, dvp
, cnp
)
622 register struct componentname
*cnp
;
624 register struct inode
*dp
;
625 struct direct newdir
;
628 newdir
.d_ino
= ip
->i_number
;
629 newdir
.d_namlen
= cnp
->cn_namelen
;
630 bcopy(cnp
->cn_nameptr
, newdir
.d_name
, (unsigned)cnp
->cn_namelen
+ 1);
631 if (dvp
->v_mount
->mnt_maxsymlinklen
> 0)
632 newdir
.d_type
= IFTODT(ip
->i_mode
);
635 # if (BYTE_ORDER == LITTLE_ENDIAN)
636 { u_char tmp
= newdir
.d_namlen
;
637 newdir
.d_namlen
= newdir
.d_type
;
638 newdir
.d_type
= tmp
; }
641 return (ufs_direnter2(dvp
, &newdir
, cnp
->cn_context
));
645 * Common entry point for directory entry removal used by ufs_direnter
649 ufs_direnter2(struct vnode
*dvp
, struct direct
*dirp
, vfs_context_t ctx
)
656 struct direct
*ep
, *nep
;
657 int error
, loc
, spacefree
;
659 char uio_buf
[ UIO_SIZEOF(1) ];
661 struct mount
*mp
=dvp
->v_mount
;
662 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
663 #endif /* REV_ENDIAN_FS */
666 newentrysize
= DIRSIZ(FSFMT(dvp
), dirp
);
668 if (dp
->i_count
== 0) {
670 * If dp->i_count is 0, then namei could find no
671 * space in the directory. Here, dp->i_offset will
672 * be on a directory block boundary and we will write the
673 * new entry into a fresh block.
675 if (dp
->i_offset
& (DIRBLKSIZ
- 1))
676 panic("ufs_direnter2: newblk");
677 dirp
->d_reclen
= DIRBLKSIZ
;
678 auio
= uio_createwithbuffer(1, dp
->i_offset
, UIO_SYSSPACE
, UIO_WRITE
,
679 &uio_buf
[0], sizeof(uio_buf
));
680 uio_addiov(auio
, CAST_USER_ADDR_T(dirp
), newentrysize
);
682 error
= ffs_write_internal(dvp
, auio
, IO_SYNC
, vfs_context_ucred(ctx
));
684 VFSTOUFS(dvp
->v_mount
)->um_mountp
->mnt_vfsstat
.f_bsize
)
685 /* XXX should grow with balloc() */
686 panic("ufs_direnter2: frag size");
688 dp
->i_size
= roundup(dp
->i_size
, DIRBLKSIZ
);
689 dp
->i_flag
|= IN_CHANGE
;
695 * If dp->i_count is non-zero, then namei found space
696 * for the new entry in the range dp->i_offset to
697 * dp->i_offset + dp->i_count in the directory.
698 * To use this space, we may have to compact the entries located
699 * there, by copying them together towards the beginning of the
700 * block, leaving the free space in one usable chunk at the end.
704 * Increase size of directory if entry eats into new space.
705 * This should never push the size past a new multiple of
708 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
710 if (dp
->i_offset
+ dp
->i_count
> dp
->i_size
)
711 dp
->i_size
= dp
->i_offset
+ dp
->i_count
;
713 * Get the block containing the space for the new directory entry.
715 if (error
= ffs_blkatoff(dvp
, (off_t
)dp
->i_offset
, &dirbuf
, &bp
))
718 * Find space for the new entry. In the simple case, the entry at
719 * offset base will have the space. If it does not, then namei
720 * arranged that compacting the region dp->i_offset to
721 * dp->i_offset + dp->i_count would yield the
724 ep
= (struct direct
*)dirbuf
;
725 dsize
= DIRSIZ(FSFMT(dvp
), ep
);
726 spacefree
= ep
->d_reclen
- dsize
;
727 for (loc
= ep
->d_reclen
; loc
< dp
->i_count
; ) {
728 nep
= (struct direct
*)(dirbuf
+ loc
);
730 /* trim the existing slot */
731 ep
->d_reclen
= dsize
;
732 ep
= (struct direct
*)((char *)ep
+ dsize
);
734 /* overwrite; nothing there; header is ours */
737 dsize
= DIRSIZ(FSFMT(dvp
), nep
);
738 spacefree
+= nep
->d_reclen
- dsize
;
739 loc
+= nep
->d_reclen
;
740 bcopy((caddr_t
)nep
, (caddr_t
)ep
, dsize
);
743 * Update the pointer fields in the previous entry (if any),
744 * copy in the new entry, and write out the block.
746 if (ep
->d_ino
== 0 ||
747 (ep
->d_ino
== WINO
&&
748 bcmp(ep
->d_name
, dirp
->d_name
, dirp
->d_namlen
) == 0)) {
749 if (spacefree
+ dsize
< newentrysize
)
750 panic("ufs_direnter2: compact1");
751 dirp
->d_reclen
= spacefree
+ dsize
;
753 if (spacefree
< newentrysize
)
754 panic("ufs_direnter2: compact2");
755 dirp
->d_reclen
= spacefree
;
756 ep
->d_reclen
= dsize
;
757 ep
= (struct direct
*)((char *)ep
+ dsize
);
759 bcopy((caddr_t
)dirp
, (caddr_t
)ep
, (u_int
)newentrysize
);
762 byte_swap_dir_block_out(bp
);
763 #endif /* REV_ENDIAN_FS */
764 if (mp
->mnt_flag
& MNT_ASYNC
) {
768 error
= VNOP_BWRITE(bp
);
770 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
771 if (!error
&& dp
->i_endoff
&& dp
->i_endoff
< dp
->i_size
)
772 error
= ffs_truncate_internal(dvp
, (off_t
)dp
->i_endoff
, IO_SYNC
, vfs_context_ucred(ctx
));
778 * Remove a directory entry after a call to namei, using
779 * the parameters which it left in nameidata. The entry
780 * dp->i_offset contains the offset into the directory of the
781 * entry to be eliminated. The dp->i_count field contains the
782 * size of the previous record in the directory. If this
783 * is 0, the first entry is being deleted, so we need only
784 * zero the inode number to mark the entry as free. If the
785 * entry is not the first in the directory, we must reclaim
786 * the space of the now empty record by adding the record size
787 * to the size of the previous entry.
790 ufs_dirremove(dvp
, cnp
)
792 struct componentname
*cnp
;
794 register struct inode
*dp
;
799 struct mount
*mp
=dvp
->v_mount
;
800 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
801 #endif /* REV_ENDIAN_FS */
805 if (cnp
->cn_flags
& DOWHITEOUT
) {
807 * Whiteout entry: set d_ino to WINO.
809 if (error
= ffs_blkatoff(dvp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
))
815 byte_swap_dir_block_out(bp
);
816 #endif /* REV_ENDIAN_FS */
817 if (mp
->mnt_flag
& MNT_ASYNC
) {
821 error
= VNOP_BWRITE(bp
);
823 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
827 if (dp
->i_count
== 0) {
829 * First entry in block: set d_ino to zero.
831 if (error
= ffs_blkatoff(dvp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
))
836 byte_swap_dir_block_out(bp
);
837 #endif /* REV_ENDIAN_FS */
838 if (mp
->mnt_flag
& MNT_ASYNC
) {
842 error
= VNOP_BWRITE(bp
);
844 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
848 * Collapse new free space into previous entry.
850 if (error
= ffs_blkatoff(dvp
, (off_t
)(dp
->i_offset
- dp
->i_count
),
853 ep
->d_reclen
+= dp
->i_reclen
;
856 byte_swap_dir_block_out(bp
);
857 #endif /* REV_ENDIAN_FS */
858 if (mp
->mnt_flag
& MNT_ASYNC
) {
862 error
= VNOP_BWRITE(bp
);
864 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
870 * Rewrite an existing directory entry to point at the inode
871 * supplied. The parameters describing the directory entry are
872 * set up by a call to namei.
875 ufs_dirrewrite(dp
, ip
, cnp
)
876 struct inode
*dp
, *ip
;
877 struct componentname
*cnp
;
881 struct vnode
*vdp
= ITOV(dp
);
884 if (error
= ffs_blkatoff(vdp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
))
886 ep
->d_ino
= ip
->i_number
;
887 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0)
888 ep
->d_type
= IFTODT(ip
->i_mode
);
890 if (vdp
->v_mount
->mnt_flag
& MNT_REVEND
)
891 byte_swap_dir_block_out(bp
);
892 #endif /* REV_ENDIAN_FS */
893 if (vdp
->v_mount
->mnt_flag
& MNT_ASYNC
) {
897 error
= VNOP_BWRITE(bp
);
899 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
904 * Check if a directory is empty or not.
905 * Inode supplied must be locked.
907 * Using a struct dirtemplate here is not precisely
908 * what we want, but better than using a struct direct.
910 * NB: does not handle corrupted directories.
913 ufs_dirempty(struct inode
*ip
, ino_t parentino
, kauth_cred_t cred
)
916 struct dirtemplate dbuf
;
917 register struct direct
*dp
= (struct direct
*)&dbuf
;
918 int error
, count
, namlen
;
920 struct vnode
*vp
=ITOV(ip
);
921 struct mount
*mp
=vp
->v_mount
;
922 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
923 #endif /* REV_ENDIAN_FS */
925 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
927 for (off
= 0; off
< ip
->i_size
; off
+= dp
->d_reclen
) {
928 error
= vn_rdwr(UIO_READ
, ITOV(ip
), (caddr_t
)dp
, MINDIRSIZ
, off
,
929 UIO_SYSSPACE32
, IO_NODELOCKED
, cred
, &count
, (struct proc
*)0);
931 * Since we read MINDIRSIZ, residual must
932 * be 0 unless we're at end of file.
934 if (error
|| count
!= 0)
936 #if 0 /*REV_ENDIAN_FS */
938 byte_swap_minidir_in(dp
);
939 #endif /* REV_ENDIAN_FS */
940 /* avoid infinite loops */
941 if (dp
->d_reclen
== 0)
943 /* skip empty entries */
944 if (dp
->d_ino
== 0 || dp
->d_ino
== WINO
)
946 /* accept only "." and ".." */
947 # if (BYTE_ORDER == LITTLE_ENDIAN)
948 if (ITOV(ip
)->v_mount
->mnt_maxsymlinklen
> 0)
949 namlen
= dp
->d_namlen
;
953 namlen
= dp
->d_namlen
;
957 if (dp
->d_name
[0] != '.')
960 * At this point namlen must be 1 or 2.
961 * 1 implies ".", 2 implies ".." if second
966 if (dp
->d_name
[1] == '.' && dp
->d_ino
== parentino
)
974 * Check if source directory is in the path of the target directory.
975 * Target is supplied locked, source is unlocked.
978 ufs_checkpath(source
, target
, cred
)
979 struct inode
*source
, *target
;
983 int error
, rootino
, namlen
;
985 struct dirtemplate dirbuf
;
988 if (target
->i_number
== source
->i_number
) {
994 if (target
->i_number
== rootino
)
998 if (vp
->v_type
!= VDIR
) {
1002 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&dirbuf
,
1003 sizeof (struct dirtemplate
), (off_t
)0, UIO_SYSSPACE32
,
1004 IO_NODELOCKED
, cred
, (int *)0, (struct proc
*)0);
1007 # if (BYTE_ORDER == LITTLE_ENDIAN)
1008 if (vp
->v_mount
->mnt_maxsymlinklen
> 0)
1009 namlen
= dirbuf
.dotdot_namlen
;
1011 namlen
= dirbuf
.dotdot_type
;
1013 namlen
= dirbuf
.dotdot_namlen
;
1016 dirbuf
.dotdot_name
[0] != '.' ||
1017 dirbuf
.dotdot_name
[1] != '.') {
1021 if (dirbuf
.dotdot_ino
== source
->i_number
) {
1025 if (dirbuf
.dotdot_ino
== rootino
)
1031 if (error
= VFS_VGET(vp
->v_mount
, (ino64_t
)dirbuf
.dotdot_ino
, &vp
, NULL
)) { /* XXX need context */
1039 if (error
== ENOTDIR
)
1040 printf("checkpath: .. not a directory\n");