]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_lookup.c
6d48ca5af2c8271af4ac2d36971b6f7035ce74ae
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
68 #include <rev_endian_fs.h>
69 #include <sys/param.h>
70 #include <sys/namei.h>
73 #include <sys/mount_internal.h>
74 #include <sys/vnode_internal.h>
75 #include <sys/quota.h>
76 #include <sys/kauth.h>
77 #include <sys/uio_internal.h>
79 #include <ufs/ufs/quota.h>
80 #include <ufs/ufs/inode.h>
81 #include <ufs/ufs/dir.h>
82 #include <ufs/ufs/ufsmount.h>
83 #include <ufs/ufs/ufs_extern.h>
84 #include <ufs/ffs/ffs_extern.h>
86 #include <ufs/ufs/ufs_byte_order.h>
87 #include <architecture/byte_order.h>
88 #endif /* REV_ENDIAN_FS */
90 extern struct nchstats nchstats
;
97 #define FSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
100 * Convert a component of a pathname into a pointer to a locked inode.
101 * This is a very central and rather complicated routine.
102 * If the file system is not maintained in a strict tree hierarchy,
103 * this can result in a deadlock situation (see comments in code below).
105 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
106 * on whether the name is to be looked up, created, renamed, or deleted.
107 * When CREATE, RENAME, or DELETE is specified, information usable in
108 * creating, renaming, or deleting a directory entry may be calculated.
109 * If flag has LOCKPARENT or'ed into it and the target of the pathname
110 * exists, lookup returns both the target and its parent directory locked.
111 * When creating or renaming and LOCKPARENT is specified, the target may
112 * not be ".". When deleting and LOCKPARENT is specified, the target may
115 * Overall outline of ufs_lookup:
117 * check accessibility of directory
118 * look for name in cache, if found, then if at end of path
119 * and deleting or creating, drop it, else return name
120 * search for name in directory, to found or notfound
122 * if creating, return locked directory, leaving info on available slots
125 * if at end of path and deleting, return information to allow delete
126 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
127 * inode and return info to allow rewrite
128 * if not at end, add name to cache; if at end and neither creating
129 * nor deleting, add name to cache
133 struct vnop_lookup_args
/* {
135 struct vnode **a_vpp;
136 struct componentname *a_cnp;
137 vfs_context_t a_context
140 register struct vnode
*vdp
; /* vnode for directory being searched */
141 register struct inode
*dp
; /* inode for directory being searched */
142 struct buf
*bp
; /* a buffer of directory entries */
143 register struct direct
*ep
; /* the current directory entry */
144 int entryoffsetinblock
; /* offset of ep in bp's buffer */
145 enum {NONE
, COMPACT
, FOUND
} slotstatus
;
146 doff_t slotoffset
; /* offset of area with free space */
147 int slotsize
; /* size of area at slotoffset */
148 int slotfreespace
; /* amount of space free in slot */
149 int slotneeded
; /* size of the entry we're seeking */
150 int numdirpasses
; /* strategy for directory search */
151 doff_t endsearch
; /* offset to end directory search */
152 doff_t prevoff
; /* prev entry dp->i_offset */
153 struct vnode
*pdp
; /* saved dp during symlink work */
154 struct vnode
*tdp
; /* returned by VFS_VGET */
155 doff_t enduseful
; /* pointer past last used dir slot */
156 u_long bmask
; /* block offset mask */
157 int wantparent
; /* 1 => wantparent or lockparent flag */
159 struct vnode
**vpp
= ap
->a_vpp
;
160 struct componentname
*cnp
= ap
->a_cnp
;
161 int flags
= cnp
->cn_flags
;
162 int nameiop
= cnp
->cn_nameiop
;
163 vfs_context_t context
= ap
->a_context
;
167 #endif /* REV_ENDIAN_FS */
170 if (cnp
->cn_namelen
> MAXNAMLEN
)
171 return (ENAMETOOLONG
);
173 cred
= vfs_context_ucred(context
);
180 wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
183 rev_endian
=(vdp
->v_mount
->mnt_flag
& MNT_REVEND
);
184 #endif /* REV_ENDIAN_FS */
187 * Check accessiblity of directory.
189 if ((dp
->i_mode
& IFMT
) != IFDIR
)
193 * We now have a segment name to search for, and a directory to search.
195 * Before tediously performing a linear scan of the directory,
196 * check the name cache to see if the directory/name pair
197 * we are looking for is known already.
199 if (error
= cache_lookup(vdp
, vpp
, cnp
)) {
205 * Suppress search for slots unless creating
206 * file and at end of pathname, in which case
207 * we watch for a place to put the new file in
208 * case it doesn't already exist.
211 slotfreespace
= slotsize
= slotneeded
= 0;
212 if ((nameiop
== CREATE
|| nameiop
== RENAME
) &&
213 (flags
& ISLASTCN
)) {
215 slotneeded
= (sizeof(struct direct
) - MAXNAMLEN
+
216 cnp
->cn_namelen
+ 3) &~ 3;
219 * If there is cached information on a previous search of
220 * this directory, pick up where we last left off.
221 * We cache only lookups as these are the most common
222 * and have the greatest payoff. Caching CREATE has little
223 * benefit as it usually must search the entire directory
224 * to determine that the entry does not exist. Caching the
225 * location of the last DELETE or RENAME has not reduced
226 * profiling time and hence has been removed in the interest
229 bmask
= VFSTOUFS(vdp
->v_mount
)->um_mountp
->mnt_vfsstat
.f_iosize
- 1;
230 if (nameiop
!= LOOKUP
|| dp
->i_diroff
== 0 ||
231 dp
->i_diroff
> dp
->i_size
) {
232 entryoffsetinblock
= 0;
236 dp
->i_offset
= dp
->i_diroff
;
237 if ((entryoffsetinblock
= dp
->i_offset
& bmask
) &&
238 (error
= ffs_blkatoff(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
)))
241 nchstats
.ncs_2passes
++;
243 prevoff
= dp
->i_offset
;
244 endsearch
= roundup(dp
->i_size
, DIRBLKSIZ
);
248 while (dp
->i_offset
< endsearch
) {
250 * If necessary, get the next directory block.
252 if ((dp
->i_offset
& bmask
) == 0) {
256 byte_swap_dir_block_out(bp
);
257 #endif /* REV_ENDIAN_FS */
260 if (error
= ffs_blkatoff(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
))
262 entryoffsetinblock
= 0;
265 * If still looking for a slot, and at a DIRBLKSIZE
266 * boundary, have to start looking for free space again.
268 if (slotstatus
== NONE
&&
269 (entryoffsetinblock
& (DIRBLKSIZ
- 1)) == 0) {
274 * Get pointer to next entry.
275 * Full validation checks are slow, but necessary.
277 ep
= (struct direct
*)((char *)buf_dataptr(bp
) + entryoffsetinblock
);
278 if (ep
->d_reclen
== 0 ||
279 ufs_dirbadentry(vdp
, ep
, entryoffsetinblock
)) {
282 ufs_dirbad(dp
, dp
->i_offset
, "mangled entry");
283 i
= DIRBLKSIZ
- (entryoffsetinblock
& (DIRBLKSIZ
- 1));
285 entryoffsetinblock
+= i
;
290 * If an appropriate sized slot has not yet been found,
291 * check to see if one is available. Also accumulate space
292 * in the current block so that we can determine if
293 * compaction is viable.
295 if (slotstatus
!= FOUND
) {
296 int size
= ep
->d_reclen
;
299 size
-= DIRSIZ(FSFMT(vdp
), ep
);
301 if (size
>= slotneeded
) {
303 slotoffset
= dp
->i_offset
;
304 slotsize
= ep
->d_reclen
;
305 } else if (slotstatus
== NONE
) {
306 slotfreespace
+= size
;
307 if (slotoffset
== -1)
308 slotoffset
= dp
->i_offset
;
309 if (slotfreespace
>= slotneeded
) {
310 slotstatus
= COMPACT
;
311 slotsize
= dp
->i_offset
+
312 ep
->d_reclen
- slotoffset
;
319 * Check for a name match.
322 # if (BYTE_ORDER == LITTLE_ENDIAN)
323 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0)
324 namlen
= ep
->d_namlen
;
328 namlen
= ep
->d_namlen
;
330 if (namlen
== cnp
->cn_namelen
&&
331 !bcmp(cnp
->cn_nameptr
, ep
->d_name
,
334 * Save directory entry's inode number and
335 * reclen in ndp->ni_ufs area, and release
338 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0 &&
339 ep
->d_type
== DT_WHT
) {
341 slotoffset
= dp
->i_offset
;
342 slotsize
= ep
->d_reclen
;
343 dp
->i_reclen
= slotsize
;
344 enduseful
= dp
->i_size
;
345 ap
->a_cnp
->cn_flags
|= ISWHITEOUT
;
349 dp
->i_ino
= ep
->d_ino
;
350 dp
->i_reclen
= ep
->d_reclen
;
353 byte_swap_dir_block_out(bp
);
354 #endif /* REV_ENDIAN_FS */
359 prevoff
= dp
->i_offset
;
360 dp
->i_offset
+= ep
->d_reclen
;
361 entryoffsetinblock
+= ep
->d_reclen
;
363 enduseful
= dp
->i_offset
;
367 * If we started in the middle of the directory and failed
368 * to find our target, we must check the beginning as well.
370 if (numdirpasses
== 2) {
373 endsearch
= dp
->i_diroff
;
379 byte_swap_dir_block_out(bp
);
380 #endif /* REV_ENDIAN_FS */
384 * If creating, and at end of pathname and current
385 * directory has not been removed, then can consider
386 * allowing file to be created.
388 if ((nameiop
== CREATE
|| nameiop
== RENAME
||
389 (nameiop
== DELETE
&&
390 (ap
->a_cnp
->cn_flags
& DOWHITEOUT
) &&
391 (ap
->a_cnp
->cn_flags
& ISWHITEOUT
))) &&
392 (flags
& ISLASTCN
) && dp
->i_nlink
!= 0) {
394 * Return an indication of where the new directory
395 * entry should be put. If we didn't find a slot,
396 * then set dp->i_count to 0 indicating
397 * that the new slot belongs at the end of the
398 * directory. If we found a slot, then the new entry
399 * can be put in the range from dp->i_offset to
400 * dp->i_offset + dp->i_count.
402 if (slotstatus
== NONE
) {
403 dp
->i_offset
= roundup(dp
->i_size
, DIRBLKSIZ
);
405 enduseful
= dp
->i_offset
;
406 } else if (nameiop
== DELETE
) {
407 dp
->i_offset
= slotoffset
;
408 if ((dp
->i_offset
& (DIRBLKSIZ
- 1)) == 0)
411 dp
->i_count
= dp
->i_offset
- prevoff
;
413 dp
->i_offset
= slotoffset
;
414 dp
->i_count
= slotsize
;
415 if (enduseful
< slotoffset
+ slotsize
)
416 enduseful
= slotoffset
+ slotsize
;
418 dp
->i_endoff
= roundup(enduseful
, DIRBLKSIZ
);
419 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
421 * We return with the directory locked, so that
422 * the parameters we set up above will still be
423 * valid if we actually decide to do a direnter().
424 * We return ni_vp == NULL to indicate that the entry
425 * does not currently exist; we leave a pointer to
426 * the (locked) directory inode in ndp->ni_dvp.
428 * NB - if the directory is unlocked, then this
429 * information cannot be used.
435 * Insert name into cache (as non-existent) if appropriate.
437 if ((cnp
->cn_flags
& MAKEENTRY
) && nameiop
!= CREATE
)
438 cache_enter(vdp
, *vpp
, cnp
);
443 if (numdirpasses
== 2)
444 nchstats
.ncs_pass2
++;
446 * Check that directory length properly reflects presence
449 if (entryoffsetinblock
+ DIRSIZ(FSFMT(vdp
), ep
) > dp
->i_size
) {
450 ufs_dirbad(dp
, dp
->i_offset
, "i_size too small");
451 dp
->i_size
= entryoffsetinblock
+ DIRSIZ(FSFMT(vdp
), ep
);
452 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
456 * Found component in pathname.
457 * If the final component of path name, save information
458 * in the cache as to where the entry was found.
460 if ((flags
& ISLASTCN
) && nameiop
== LOOKUP
)
461 dp
->i_diroff
= dp
->i_offset
&~ (DIRBLKSIZ
- 1);
464 * If deleting, and at end of pathname, return
465 * parameters which can be used to remove file.
466 * If the wantparent flag isn't set, we return only
467 * the directory (in ndp->ni_dvp), otherwise we go
468 * on and lock the inode, being careful with ".".
470 if (nameiop
== DELETE
&& (flags
& ISLASTCN
)) {
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
) {
487 if (error
= ffs_vget_internal(vdp
->v_mount
, dp
->i_ino
, &tdp
, vdp
, cnp
, 0, 0))
494 * If rewriting (RENAME), return the inode and the
495 * information required to rewrite the present directory
496 * Must get inode of directory entry to verify it's a
497 * regular file, or empty directory.
499 if (nameiop
== RENAME
&& wantparent
&& (flags
& ISLASTCN
)) {
501 * Careful about locking second inode.
502 * This can only occur if the target is ".".
504 if (dp
->i_number
== dp
->i_ino
) {
508 if (error
= ffs_vget_internal(vdp
->v_mount
, dp
->i_ino
, &tdp
, vdp
, cnp
, 0, 0))
516 * Step through the translation in the name. We do not `vnode_put' the
517 * directory because we may need it again if a symbolic link
518 * is relative to the current directory. Instead we save it
519 * unlocked as "pdp". We must get the target inode before unlocking
520 * the directory to insure that the inode will not be removed
521 * before we get it. We prevent deadlock by always fetching
522 * inodes from the root, moving down the directory tree. Thus
523 * when following backward pointers ".." we must unlock the
524 * parent directory before getting the requested directory.
525 * There is a potential race condition here if both the current
526 * and parent directories are removed before the VFS_VGET for the
527 * inode associated with ".." returns. We hope that this occurs
528 * infrequently since we cannot avoid this race condition without
529 * implementing a sophisticated deadlock detection algorithm.
530 * Note also that this simple deadlock detection scheme will not
531 * work if the file system has any hard links other than ".."
532 * that point backwards in the directory structure.
535 if (flags
& ISDOTDOT
) {
536 if (error
= ffs_vget_internal(vdp
->v_mount
, dp
->i_ino
, &tdp
, vdp
, cnp
, 0, 0)) {
540 } else if (dp
->i_number
== dp
->i_ino
) {
541 vnode_get(vdp
); /* we want ourself, ie "." */
544 if (error
= ffs_vget_internal(vdp
->v_mount
, dp
->i_ino
, &tdp
, vdp
, cnp
, 0, 0))
555 ufs_dirbad(ip
, offset
, how
)
562 mp
= ITOV(ip
)->v_mount
;
563 (void)printf("%s: bad dir ino %d at offset %d: %s\n",
564 mp
->mnt_vfsstat
.f_mntonname
, ip
->i_number
, offset
, how
);
566 if ((mp
->mnt_vfsstat
.f_flags
& MNT_RDONLY
) == 0)
572 * Do consistency checking on a directory entry:
573 * record length must be multiple of 4
574 * entry must fit in rest of its DIRBLKSIZ block
575 * record must be large enough to contain entry
576 * name is not longer than MAXNAMLEN
577 * name must be as long as advertised, and null terminated
580 ufs_dirbadentry(dp
, ep
, entryoffsetinblock
)
582 register struct direct
*ep
;
583 int entryoffsetinblock
;
589 struct ufsmount
*ump
= VFSTOUFS(dp
->v_mount
);
591 # if (BYTE_ORDER == LITTLE_ENDIAN)
592 if (dp
->v_mount
->mnt_maxsymlinklen
> 0)
593 namlen
= ep
->d_namlen
;
597 namlen
= ep
->d_namlen
;
599 if ((ep
->d_reclen
& 0x3) != 0 ||
600 ep
->d_reclen
> DIRBLKSIZ
- (entryoffsetinblock
& (DIRBLKSIZ
- 1)) ||
601 ep
->d_reclen
< DIRSIZ(FSFMT(dp
), ep
) || namlen
> MAXNAMLEN
) {
603 printf("First bad\n");
608 for (i
= 0; i
< namlen
; i
++)
609 if (ep
->d_name
[i
] == '\0') {
611 printf("Second bad\n");
618 maxino
= fs
->fs_ncg
* fs
->fs_ipg
;
619 if (ep
->d_ino
> maxino
) {
620 printf("Third bad\n");
630 * Write a directory entry after a call to namei, using the parameters
631 * that it left in nameidata. The argument ip is the inode which the new
632 * directory entry will refer to. Dvp is a pointer to the directory to
633 * be written, which was left locked by namei. Remaining parameters
634 * (dp->i_offset, dp->i_count) indicate how the space for the new
635 * entry is to be obtained.
638 ufs_direnter(ip
, dvp
, cnp
)
641 register struct componentname
*cnp
;
643 register struct inode
*dp
;
644 struct direct newdir
;
647 newdir
.d_ino
= ip
->i_number
;
648 newdir
.d_namlen
= cnp
->cn_namelen
;
649 bcopy(cnp
->cn_nameptr
, newdir
.d_name
, (unsigned)cnp
->cn_namelen
+ 1);
650 if (dvp
->v_mount
->mnt_maxsymlinklen
> 0)
651 newdir
.d_type
= IFTODT(ip
->i_mode
);
654 # if (BYTE_ORDER == LITTLE_ENDIAN)
655 { u_char tmp
= newdir
.d_namlen
;
656 newdir
.d_namlen
= newdir
.d_type
;
657 newdir
.d_type
= tmp
; }
660 return (ufs_direnter2(dvp
, &newdir
, cnp
->cn_context
));
664 * Common entry point for directory entry removal used by ufs_direnter
668 ufs_direnter2(struct vnode
*dvp
, struct direct
*dirp
, vfs_context_t ctx
)
675 struct direct
*ep
, *nep
;
676 int error
, loc
, spacefree
;
678 char uio_buf
[ UIO_SIZEOF(1) ];
680 struct mount
*mp
=dvp
->v_mount
;
681 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
682 #endif /* REV_ENDIAN_FS */
685 newentrysize
= DIRSIZ(FSFMT(dvp
), dirp
);
687 if (dp
->i_count
== 0) {
689 * If dp->i_count is 0, then namei could find no
690 * space in the directory. Here, dp->i_offset will
691 * be on a directory block boundary and we will write the
692 * new entry into a fresh block.
694 if (dp
->i_offset
& (DIRBLKSIZ
- 1))
695 panic("ufs_direnter2: newblk");
696 dirp
->d_reclen
= DIRBLKSIZ
;
697 auio
= uio_createwithbuffer(1, dp
->i_offset
, UIO_SYSSPACE
, UIO_WRITE
,
698 &uio_buf
[0], sizeof(uio_buf
));
699 uio_addiov(auio
, CAST_USER_ADDR_T(dirp
), newentrysize
);
701 error
= ffs_write_internal(dvp
, auio
, IO_SYNC
, vfs_context_ucred(ctx
));
703 VFSTOUFS(dvp
->v_mount
)->um_mountp
->mnt_vfsstat
.f_bsize
)
704 /* XXX should grow with balloc() */
705 panic("ufs_direnter2: frag size");
707 dp
->i_size
= roundup(dp
->i_size
, DIRBLKSIZ
);
708 dp
->i_flag
|= IN_CHANGE
;
714 * If dp->i_count is non-zero, then namei found space
715 * for the new entry in the range dp->i_offset to
716 * dp->i_offset + dp->i_count in the directory.
717 * To use this space, we may have to compact the entries located
718 * there, by copying them together towards the beginning of the
719 * block, leaving the free space in one usable chunk at the end.
723 * Increase size of directory if entry eats into new space.
724 * This should never push the size past a new multiple of
727 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
729 if (dp
->i_offset
+ dp
->i_count
> dp
->i_size
)
730 dp
->i_size
= dp
->i_offset
+ dp
->i_count
;
732 * Get the block containing the space for the new directory entry.
734 if (error
= ffs_blkatoff(dvp
, (off_t
)dp
->i_offset
, &dirbuf
, &bp
))
737 * Find space for the new entry. In the simple case, the entry at
738 * offset base will have the space. If it does not, then namei
739 * arranged that compacting the region dp->i_offset to
740 * dp->i_offset + dp->i_count would yield the
743 ep
= (struct direct
*)dirbuf
;
744 dsize
= DIRSIZ(FSFMT(dvp
), ep
);
745 spacefree
= ep
->d_reclen
- dsize
;
746 for (loc
= ep
->d_reclen
; loc
< dp
->i_count
; ) {
747 nep
= (struct direct
*)(dirbuf
+ loc
);
749 /* trim the existing slot */
750 ep
->d_reclen
= dsize
;
751 ep
= (struct direct
*)((char *)ep
+ dsize
);
753 /* overwrite; nothing there; header is ours */
756 dsize
= DIRSIZ(FSFMT(dvp
), nep
);
757 spacefree
+= nep
->d_reclen
- dsize
;
758 loc
+= nep
->d_reclen
;
759 bcopy((caddr_t
)nep
, (caddr_t
)ep
, dsize
);
762 * Update the pointer fields in the previous entry (if any),
763 * copy in the new entry, and write out the block.
765 if (ep
->d_ino
== 0 ||
766 (ep
->d_ino
== WINO
&&
767 bcmp(ep
->d_name
, dirp
->d_name
, dirp
->d_namlen
) == 0)) {
768 if (spacefree
+ dsize
< newentrysize
)
769 panic("ufs_direnter2: compact1");
770 dirp
->d_reclen
= spacefree
+ dsize
;
772 if (spacefree
< newentrysize
)
773 panic("ufs_direnter2: compact2");
774 dirp
->d_reclen
= spacefree
;
775 ep
->d_reclen
= dsize
;
776 ep
= (struct direct
*)((char *)ep
+ dsize
);
778 bcopy((caddr_t
)dirp
, (caddr_t
)ep
, (u_int
)newentrysize
);
781 byte_swap_dir_block_out(bp
);
782 #endif /* REV_ENDIAN_FS */
783 if (mp
->mnt_flag
& MNT_ASYNC
) {
787 error
= VNOP_BWRITE(bp
);
789 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
790 if (!error
&& dp
->i_endoff
&& dp
->i_endoff
< dp
->i_size
)
791 error
= ffs_truncate_internal(dvp
, (off_t
)dp
->i_endoff
, IO_SYNC
, vfs_context_ucred(ctx
));
797 * Remove a directory entry after a call to namei, using
798 * the parameters which it left in nameidata. The entry
799 * dp->i_offset contains the offset into the directory of the
800 * entry to be eliminated. The dp->i_count field contains the
801 * size of the previous record in the directory. If this
802 * is 0, the first entry is being deleted, so we need only
803 * zero the inode number to mark the entry as free. If the
804 * entry is not the first in the directory, we must reclaim
805 * the space of the now empty record by adding the record size
806 * to the size of the previous entry.
809 ufs_dirremove(dvp
, cnp
)
811 struct componentname
*cnp
;
813 register struct inode
*dp
;
818 struct mount
*mp
=dvp
->v_mount
;
819 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
820 #endif /* REV_ENDIAN_FS */
824 if (cnp
->cn_flags
& DOWHITEOUT
) {
826 * Whiteout entry: set d_ino to WINO.
828 if (error
= ffs_blkatoff(dvp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
))
834 byte_swap_dir_block_out(bp
);
835 #endif /* REV_ENDIAN_FS */
836 if (mp
->mnt_flag
& MNT_ASYNC
) {
840 error
= VNOP_BWRITE(bp
);
842 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
846 if (dp
->i_count
== 0) {
848 * First entry in block: set d_ino to zero.
850 if (error
= ffs_blkatoff(dvp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
))
855 byte_swap_dir_block_out(bp
);
856 #endif /* REV_ENDIAN_FS */
857 if (mp
->mnt_flag
& MNT_ASYNC
) {
861 error
= VNOP_BWRITE(bp
);
863 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
867 * Collapse new free space into previous entry.
869 if (error
= ffs_blkatoff(dvp
, (off_t
)(dp
->i_offset
- dp
->i_count
),
872 ep
->d_reclen
+= dp
->i_reclen
;
875 byte_swap_dir_block_out(bp
);
876 #endif /* REV_ENDIAN_FS */
877 if (mp
->mnt_flag
& MNT_ASYNC
) {
881 error
= VNOP_BWRITE(bp
);
883 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
889 * Rewrite an existing directory entry to point at the inode
890 * supplied. The parameters describing the directory entry are
891 * set up by a call to namei.
894 ufs_dirrewrite(dp
, ip
, cnp
)
895 struct inode
*dp
, *ip
;
896 struct componentname
*cnp
;
900 struct vnode
*vdp
= ITOV(dp
);
903 if (error
= ffs_blkatoff(vdp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
))
905 ep
->d_ino
= ip
->i_number
;
906 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0)
907 ep
->d_type
= IFTODT(ip
->i_mode
);
909 if (vdp
->v_mount
->mnt_flag
& MNT_REVEND
)
910 byte_swap_dir_block_out(bp
);
911 #endif /* REV_ENDIAN_FS */
912 if (vdp
->v_mount
->mnt_flag
& MNT_ASYNC
) {
916 error
= VNOP_BWRITE(bp
);
918 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
923 * Check if a directory is empty or not.
924 * Inode supplied must be locked.
926 * Using a struct dirtemplate here is not precisely
927 * what we want, but better than using a struct direct.
929 * NB: does not handle corrupted directories.
932 ufs_dirempty(struct inode
*ip
, ino_t parentino
, kauth_cred_t cred
)
935 struct dirtemplate dbuf
;
936 register struct direct
*dp
= (struct direct
*)&dbuf
;
937 int error
, count
, namlen
;
939 struct vnode
*vp
=ITOV(ip
);
940 struct mount
*mp
=vp
->v_mount
;
941 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
942 #endif /* REV_ENDIAN_FS */
944 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
946 for (off
= 0; off
< ip
->i_size
; off
+= dp
->d_reclen
) {
947 error
= vn_rdwr(UIO_READ
, ITOV(ip
), (caddr_t
)dp
, MINDIRSIZ
, off
,
948 UIO_SYSSPACE32
, IO_NODELOCKED
, cred
, &count
, (struct proc
*)0);
950 * Since we read MINDIRSIZ, residual must
951 * be 0 unless we're at end of file.
953 if (error
|| count
!= 0)
955 #if 0 /*REV_ENDIAN_FS */
957 byte_swap_minidir_in(dp
);
958 #endif /* REV_ENDIAN_FS */
959 /* avoid infinite loops */
960 if (dp
->d_reclen
== 0)
962 /* skip empty entries */
963 if (dp
->d_ino
== 0 || dp
->d_ino
== WINO
)
965 /* accept only "." and ".." */
966 # if (BYTE_ORDER == LITTLE_ENDIAN)
967 if (ITOV(ip
)->v_mount
->mnt_maxsymlinklen
> 0)
968 namlen
= dp
->d_namlen
;
972 namlen
= dp
->d_namlen
;
976 if (dp
->d_name
[0] != '.')
979 * At this point namlen must be 1 or 2.
980 * 1 implies ".", 2 implies ".." if second
985 if (dp
->d_name
[1] == '.' && dp
->d_ino
== parentino
)
993 * Check if source directory is in the path of the target directory.
994 * Target is supplied locked, source is unlocked.
997 ufs_checkpath(source
, target
, cred
)
998 struct inode
*source
, *target
;
1002 int error
, rootino
, namlen
;
1004 struct dirtemplate dirbuf
;
1007 if (target
->i_number
== source
->i_number
) {
1013 if (target
->i_number
== rootino
)
1017 if (vp
->v_type
!= VDIR
) {
1021 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&dirbuf
,
1022 sizeof (struct dirtemplate
), (off_t
)0, UIO_SYSSPACE32
,
1023 IO_NODELOCKED
, cred
, (int *)0, (struct proc
*)0);
1026 # if (BYTE_ORDER == LITTLE_ENDIAN)
1027 if (vp
->v_mount
->mnt_maxsymlinklen
> 0)
1028 namlen
= dirbuf
.dotdot_namlen
;
1030 namlen
= dirbuf
.dotdot_type
;
1032 namlen
= dirbuf
.dotdot_namlen
;
1035 dirbuf
.dotdot_name
[0] != '.' ||
1036 dirbuf
.dotdot_name
[1] != '.') {
1040 if (dirbuf
.dotdot_ino
== source
->i_number
) {
1044 if (dirbuf
.dotdot_ino
== rootino
)
1050 if (error
= VFS_VGET(vp
->v_mount
, (ino64_t
)dirbuf
.dotdot_ino
, &vp
, NULL
)) { /* XXX need context */
1058 if (error
== ENOTDIR
)
1059 printf("checkpath: .. not a directory\n");