]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_lookup.c
a2583678bc549fef47bdec991ace0e09fd0a94e8
2 * Copyright (c) 2000 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.h>
68 #include <sys/vnode.h>
70 #include <ufs/ufs/quota.h>
71 #include <ufs/ufs/inode.h>
72 #include <ufs/ufs/dir.h>
73 #include <ufs/ufs/ufsmount.h>
74 #include <ufs/ufs/ufs_extern.h>
76 #include <ufs/ufs/ufs_byte_order.h>
77 #include <architecture/byte_order.h>
78 #endif /* REV_ENDIAN_FS */
80 extern struct nchstats nchstats
;
87 #define FSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
90 * Convert a component of a pathname into a pointer to a locked inode.
91 * This is a very central and rather complicated routine.
92 * If the file system is not maintained in a strict tree hierarchy,
93 * this can result in a deadlock situation (see comments in code below).
95 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
96 * on whether the name is to be looked up, created, renamed, or deleted.
97 * When CREATE, RENAME, or DELETE is specified, information usable in
98 * creating, renaming, or deleting a directory entry may be calculated.
99 * If flag has LOCKPARENT or'ed into it and the target of the pathname
100 * exists, lookup returns both the target and its parent directory locked.
101 * When creating or renaming and LOCKPARENT is specified, the target may
102 * not be ".". When deleting and LOCKPARENT is specified, the target may
103 * be "."., but the caller must check to ensure it does an vrele and vput
104 * instead of two vputs.
106 * Overall outline of ufs_lookup:
108 * check accessibility of directory
109 * look for name in cache, if found, then if at end of path
110 * and deleting or creating, drop it, else return name
111 * search for name in directory, to found or notfound
113 * if creating, return locked directory, leaving info on available slots
116 * if at end of path and deleting, return information to allow delete
117 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
118 * inode and return info to allow rewrite
119 * if not at end, add name to cache; if at end and neither creating
120 * nor deleting, add name to cache
124 struct vop_lookup_args
/* {
126 struct vnode **a_vpp;
127 struct componentname *a_cnp;
130 register struct vnode
*vdp
; /* vnode for directory being searched */
131 register struct inode
*dp
; /* inode for directory being searched */
132 struct buf
*bp
; /* a buffer of directory entries */
133 register struct direct
*ep
; /* the current directory entry */
134 int entryoffsetinblock
; /* offset of ep in bp's buffer */
135 enum {NONE
, COMPACT
, FOUND
} slotstatus
;
136 doff_t slotoffset
; /* offset of area with free space */
137 int slotsize
; /* size of area at slotoffset */
138 int slotfreespace
; /* amount of space free in slot */
139 int slotneeded
; /* size of the entry we're seeking */
140 int numdirpasses
; /* strategy for directory search */
141 doff_t endsearch
; /* offset to end directory search */
142 doff_t prevoff
; /* prev entry dp->i_offset */
143 struct vnode
*pdp
; /* saved dp during symlink work */
144 struct vnode
*tdp
; /* returned by VFS_VGET */
145 doff_t enduseful
; /* pointer past last used dir slot */
146 u_long bmask
; /* block offset mask */
147 int lockparent
; /* 1 => lockparent flag is set */
148 int wantparent
; /* 1 => wantparent or lockparent flag */
150 struct vnode
**vpp
= ap
->a_vpp
;
151 struct componentname
*cnp
= ap
->a_cnp
;
152 struct ucred
*cred
= cnp
->cn_cred
;
153 int flags
= cnp
->cn_flags
;
154 int nameiop
= cnp
->cn_nameiop
;
155 struct proc
*p
= cnp
->cn_proc
;
158 #endif /* REV_ENDIAN_FS */
166 lockparent
= flags
& LOCKPARENT
;
167 wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
169 rev_endian
=(vdp
->v_mount
->mnt_flag
& MNT_REVEND
);
170 #endif /* REV_ENDIAN_FS */
173 * Check accessiblity of directory.
175 if ((dp
->i_mode
& IFMT
) != IFDIR
)
177 if (error
= VOP_ACCESS(vdp
, VEXEC
, cred
, cnp
->cn_proc
))
179 if ((flags
& ISLASTCN
) && (vdp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
180 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
))
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
)) {
191 int vpid
; /* capability number of vnode */
196 * Get the next vnode in the path.
197 * See comment below starting `Step through' for
198 * an explaination of the locking protocol.
204 if (pdp
== vdp
) { /* lookup on "." */
207 } else if (flags
& ISDOTDOT
) {
208 VOP_UNLOCK(pdp
, 0, p
);
209 error
= vget(vdp
, LK_EXCLUSIVE
, p
);
210 if (!error
&& lockparent
&& (flags
& ISLASTCN
))
211 error
= vn_lock(pdp
, LK_EXCLUSIVE
, p
);
213 error
= vget(vdp
, LK_EXCLUSIVE
, p
);
214 if (!lockparent
|| error
|| !(flags
& ISLASTCN
))
215 VOP_UNLOCK(pdp
, 0, p
);
218 * Check that the capability number did not change
219 * while we were waiting for the lock.
222 if (vpid
== vdp
->v_id
)
225 if (lockparent
&& pdp
!= vdp
&& (flags
& ISLASTCN
))
226 VOP_UNLOCK(pdp
, 0, p
);
228 if (error
= vn_lock(pdp
, LK_EXCLUSIVE
, p
))
236 * Suppress search for slots unless creating
237 * file and at end of pathname, in which case
238 * we watch for a place to put the new file in
239 * case it doesn't already exist.
242 slotfreespace
= slotsize
= slotneeded
= 0;
243 if ((nameiop
== CREATE
|| nameiop
== RENAME
) &&
244 (flags
& ISLASTCN
)) {
246 slotneeded
= (sizeof(struct direct
) - MAXNAMLEN
+
247 cnp
->cn_namelen
+ 3) &~ 3;
251 * If there is cached information on a previous search of
252 * this directory, pick up where we last left off.
253 * We cache only lookups as these are the most common
254 * and have the greatest payoff. Caching CREATE has little
255 * benefit as it usually must search the entire directory
256 * to determine that the entry does not exist. Caching the
257 * location of the last DELETE or RENAME has not reduced
258 * profiling time and hence has been removed in the interest
261 bmask
= VFSTOUFS(vdp
->v_mount
)->um_mountp
->mnt_stat
.f_iosize
- 1;
262 if (nameiop
!= LOOKUP
|| dp
->i_diroff
== 0 ||
263 dp
->i_diroff
> dp
->i_size
) {
264 entryoffsetinblock
= 0;
268 dp
->i_offset
= dp
->i_diroff
;
269 if ((entryoffsetinblock
= dp
->i_offset
& bmask
) &&
270 (error
= VOP_BLKATOFF(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
)))
273 nchstats
.ncs_2passes
++;
275 prevoff
= dp
->i_offset
;
276 endsearch
= roundup(dp
->i_size
, DIRBLKSIZ
);
280 while (dp
->i_offset
< endsearch
) {
282 * If necessary, get the next directory block.
284 if ((dp
->i_offset
& bmask
) == 0) {
288 byte_swap_dir_block_out(bp
);
289 #endif /* REV_ENDIAN_FS */
293 VOP_BLKATOFF(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
))
295 entryoffsetinblock
= 0;
298 * If still looking for a slot, and at a DIRBLKSIZE
299 * boundary, have to start looking for free space again.
301 if (slotstatus
== NONE
&&
302 (entryoffsetinblock
& (DIRBLKSIZ
- 1)) == 0) {
307 * Get pointer to next entry.
308 * Full validation checks are slow, so we only check
309 * enough to insure forward progress through the
310 * directory. Complete checks can be run by patching
311 * "dirchk" to be true.
313 ep
= (struct direct
*)((char *)bp
->b_data
+ entryoffsetinblock
);
314 if (ep
->d_reclen
== 0 ||
315 dirchk
&& ufs_dirbadentry(vdp
, ep
, entryoffsetinblock
)) {
318 ufs_dirbad(dp
, dp
->i_offset
, "mangled entry");
319 i
= DIRBLKSIZ
- (entryoffsetinblock
& (DIRBLKSIZ
- 1));
321 entryoffsetinblock
+= i
;
326 * If an appropriate sized slot has not yet been found,
327 * check to see if one is available. Also accumulate space
328 * in the current block so that we can determine if
329 * compaction is viable.
331 if (slotstatus
!= FOUND
) {
332 int size
= ep
->d_reclen
;
335 size
-= DIRSIZ(FSFMT(vdp
), ep
);
337 if (size
>= slotneeded
) {
339 slotoffset
= dp
->i_offset
;
340 slotsize
= ep
->d_reclen
;
341 } else if (slotstatus
== NONE
) {
342 slotfreespace
+= size
;
343 if (slotoffset
== -1)
344 slotoffset
= dp
->i_offset
;
345 if (slotfreespace
>= slotneeded
) {
346 slotstatus
= COMPACT
;
347 slotsize
= dp
->i_offset
+
348 ep
->d_reclen
- slotoffset
;
355 * Check for a name match.
358 # if (BYTE_ORDER == LITTLE_ENDIAN)
359 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0)
360 namlen
= ep
->d_namlen
;
364 namlen
= ep
->d_namlen
;
366 if (namlen
== cnp
->cn_namelen
&&
367 !bcmp(cnp
->cn_nameptr
, ep
->d_name
,
370 * Save directory entry's inode number and
371 * reclen in ndp->ni_ufs area, and release
374 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0 &&
375 ep
->d_type
== DT_WHT
) {
377 slotoffset
= dp
->i_offset
;
378 slotsize
= ep
->d_reclen
;
379 dp
->i_reclen
= slotsize
;
380 enduseful
= dp
->i_size
;
381 ap
->a_cnp
->cn_flags
|= ISWHITEOUT
;
385 dp
->i_ino
= ep
->d_ino
;
386 dp
->i_reclen
= ep
->d_reclen
;
389 byte_swap_dir_block_out(bp
);
390 #endif /* REV_ENDIAN_FS */
395 prevoff
= dp
->i_offset
;
396 dp
->i_offset
+= ep
->d_reclen
;
397 entryoffsetinblock
+= ep
->d_reclen
;
399 enduseful
= dp
->i_offset
;
403 * If we started in the middle of the directory and failed
404 * to find our target, we must check the beginning as well.
406 if (numdirpasses
== 2) {
409 endsearch
= dp
->i_diroff
;
415 byte_swap_dir_block_out(bp
);
416 #endif /* REV_ENDIAN_FS */
420 * If creating, and at end of pathname and current
421 * directory has not been removed, then can consider
422 * allowing file to be created.
424 if ((nameiop
== CREATE
|| nameiop
== RENAME
||
425 (nameiop
== DELETE
&&
426 (ap
->a_cnp
->cn_flags
& DOWHITEOUT
) &&
427 (ap
->a_cnp
->cn_flags
& ISWHITEOUT
))) &&
428 (flags
& ISLASTCN
) && dp
->i_nlink
!= 0) {
430 * Access for write is interpreted as allowing
431 * creation of files in the directory.
433 if (error
= VOP_ACCESS(vdp
, VWRITE
, cred
, cnp
->cn_proc
))
436 * Return an indication of where the new directory
437 * entry should be put. If we didn't find a slot,
438 * then set dp->i_count to 0 indicating
439 * that the new slot belongs at the end of the
440 * directory. If we found a slot, then the new entry
441 * can be put in the range from dp->i_offset to
442 * dp->i_offset + dp->i_count.
444 if (slotstatus
== NONE
) {
445 dp
->i_offset
= roundup(dp
->i_size
, DIRBLKSIZ
);
447 enduseful
= dp
->i_offset
;
448 } else if (nameiop
== DELETE
) {
449 dp
->i_offset
= slotoffset
;
450 if ((dp
->i_offset
& (DIRBLKSIZ
- 1)) == 0)
453 dp
->i_count
= dp
->i_offset
- prevoff
;
455 dp
->i_offset
= slotoffset
;
456 dp
->i_count
= slotsize
;
457 if (enduseful
< slotoffset
+ slotsize
)
458 enduseful
= slotoffset
+ slotsize
;
460 dp
->i_endoff
= roundup(enduseful
, DIRBLKSIZ
);
461 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
463 * We return with the directory locked, so that
464 * the parameters we set up above will still be
465 * valid if we actually decide to do a direnter().
466 * We return ni_vp == NULL to indicate that the entry
467 * does not currently exist; we leave a pointer to
468 * the (locked) directory inode in ndp->ni_dvp.
469 * The pathname buffer is saved so that the name
470 * can be obtained later.
472 * NB - if the directory is unlocked, then this
473 * information cannot be used.
475 cnp
->cn_flags
|= SAVENAME
;
477 VOP_UNLOCK(vdp
, 0, p
);
478 return (EJUSTRETURN
);
481 * Insert name into cache (as non-existent) if appropriate.
483 if ((cnp
->cn_flags
& MAKEENTRY
) && nameiop
!= CREATE
)
484 cache_enter(vdp
, *vpp
, cnp
);
488 if (numdirpasses
== 2)
489 nchstats
.ncs_pass2
++;
491 * Check that directory length properly reflects presence
494 if (entryoffsetinblock
+ DIRSIZ(FSFMT(vdp
), ep
) > dp
->i_size
) {
495 ufs_dirbad(dp
, dp
->i_offset
, "i_size too small");
496 dp
->i_size
= entryoffsetinblock
+ DIRSIZ(FSFMT(vdp
), ep
);
497 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
501 * Found component in pathname.
502 * If the final component of path name, save information
503 * in the cache as to where the entry was found.
505 if ((flags
& ISLASTCN
) && nameiop
== LOOKUP
)
506 dp
->i_diroff
= dp
->i_offset
&~ (DIRBLKSIZ
- 1);
509 * If deleting, and at end of pathname, return
510 * parameters which can be used to remove file.
511 * If the wantparent flag isn't set, we return only
512 * the directory (in ndp->ni_dvp), otherwise we go
513 * on and lock the inode, being careful with ".".
515 if (nameiop
== DELETE
&& (flags
& ISLASTCN
)) {
517 * Write access to directory required to delete files.
519 if (error
= VOP_ACCESS(vdp
, VWRITE
, cred
, cnp
->cn_proc
))
522 * Return pointer to current entry in dp->i_offset,
523 * and distance past previous entry (if there
524 * is a previous entry in this block) in dp->i_count.
525 * Save directory inode pointer in ndp->ni_dvp for dirremove().
527 if ((dp
->i_offset
& (DIRBLKSIZ
- 1)) == 0)
530 dp
->i_count
= dp
->i_offset
- prevoff
;
531 if (dp
->i_number
== dp
->i_ino
) {
536 if (error
= VFS_VGET(vdp
->v_mount
, dp
->i_ino
, &tdp
))
539 * If directory is "sticky", then user must own
540 * the directory, or the file in it, else she
541 * may not delete it (unless she's root). This
542 * implements append-only directories.
544 if ((dp
->i_mode
& ISVTX
) &&
546 cred
->cr_uid
!= dp
->i_uid
&&
547 tdp
->v_type
!= VLNK
&&
548 VTOI(tdp
)->i_uid
!= cred
->cr_uid
) {
554 VOP_UNLOCK(vdp
, 0, p
);
559 * If rewriting (RENAME), return the inode and the
560 * information required to rewrite the present directory
561 * Must get inode of directory entry to verify it's a
562 * regular file, or empty directory.
564 if (nameiop
== RENAME
&& wantparent
&& (flags
& ISLASTCN
)) {
565 if (error
= VOP_ACCESS(vdp
, VWRITE
, cred
, cnp
->cn_proc
))
568 * Careful about locking second inode.
569 * This can only occur if the target is ".".
571 if (dp
->i_number
== dp
->i_ino
)
573 if (error
= VFS_VGET(vdp
->v_mount
, dp
->i_ino
, &tdp
))
576 cnp
->cn_flags
|= SAVENAME
;
578 VOP_UNLOCK(vdp
, 0, p
);
583 * Step through the translation in the name. We do not `vput' the
584 * directory because we may need it again if a symbolic link
585 * is relative to the current directory. Instead we save it
586 * unlocked as "pdp". We must get the target inode before unlocking
587 * the directory to insure that the inode will not be removed
588 * before we get it. We prevent deadlock by always fetching
589 * inodes from the root, moving down the directory tree. Thus
590 * when following backward pointers ".." we must unlock the
591 * parent directory before getting the requested directory.
592 * There is a potential race condition here if both the current
593 * and parent directories are removed before the VFS_VGET for the
594 * inode associated with ".." returns. We hope that this occurs
595 * infrequently since we cannot avoid this race condition without
596 * implementing a sophisticated deadlock detection algorithm.
597 * Note also that this simple deadlock detection scheme will not
598 * work if the file system has any hard links other than ".."
599 * that point backwards in the directory structure.
602 if (flags
& ISDOTDOT
) {
603 VOP_UNLOCK(pdp
, 0, p
); /* race to get the inode */
604 if (error
= VFS_VGET(vdp
->v_mount
, dp
->i_ino
, &tdp
)) {
605 vn_lock(pdp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
608 if (lockparent
&& (flags
& ISLASTCN
) &&
609 (error
= vn_lock(pdp
, LK_EXCLUSIVE
, p
))) {
614 } else if (dp
->i_number
== dp
->i_ino
) {
615 VREF(vdp
); /* we want ourself, ie "." */
618 if (error
= VFS_VGET(vdp
->v_mount
, dp
->i_ino
, &tdp
))
620 if (!lockparent
|| !(flags
& ISLASTCN
))
621 VOP_UNLOCK(pdp
, 0, p
);
626 * Insert name into cache if appropriate.
628 if (cnp
->cn_flags
& MAKEENTRY
)
629 cache_enter(vdp
, *vpp
, cnp
);
634 ufs_dirbad(ip
, offset
, how
)
641 mp
= ITOV(ip
)->v_mount
;
642 (void)printf("%s: bad dir ino %d at offset %d: %s\n",
643 mp
->mnt_stat
.f_mntonname
, ip
->i_number
, offset
, how
);
644 if ((mp
->mnt_stat
.f_flags
& MNT_RDONLY
) == 0)
649 * Do consistency checking on a directory entry:
650 * record length must be multiple of 4
651 * entry must fit in rest of its DIRBLKSIZ block
652 * record must be large enough to contain entry
653 * name is not longer than MAXNAMLEN
654 * name must be as long as advertised, and null terminated
657 ufs_dirbadentry(dp
, ep
, entryoffsetinblock
)
659 register struct direct
*ep
;
660 int entryoffsetinblock
;
665 # if (BYTE_ORDER == LITTLE_ENDIAN)
666 if (dp
->v_mount
->mnt_maxsymlinklen
> 0)
667 namlen
= ep
->d_namlen
;
671 namlen
= ep
->d_namlen
;
673 if ((ep
->d_reclen
& 0x3) != 0 ||
674 ep
->d_reclen
> DIRBLKSIZ
- (entryoffsetinblock
& (DIRBLKSIZ
- 1)) ||
675 ep
->d_reclen
< DIRSIZ(FSFMT(dp
), ep
) || namlen
> MAXNAMLEN
) {
677 printf("First bad\n");
682 for (i
= 0; i
< namlen
; i
++)
683 if (ep
->d_name
[i
] == '\0') {
685 printf("Second bad\n");
696 * Write a directory entry after a call to namei, using the parameters
697 * that it left in nameidata. The argument ip is the inode which the new
698 * directory entry will refer to. Dvp is a pointer to the directory to
699 * be written, which was left locked by namei. Remaining parameters
700 * (dp->i_offset, dp->i_count) indicate how the space for the new
701 * entry is to be obtained.
704 ufs_direnter(ip
, dvp
, cnp
)
707 register struct componentname
*cnp
;
709 register struct inode
*dp
;
710 struct direct newdir
;
713 if ((cnp
->cn_flags
& SAVENAME
) == 0)
714 panic("direnter: missing name");
717 newdir
.d_ino
= ip
->i_number
;
718 newdir
.d_namlen
= cnp
->cn_namelen
;
719 bcopy(cnp
->cn_nameptr
, newdir
.d_name
, (unsigned)cnp
->cn_namelen
+ 1);
720 if (dvp
->v_mount
->mnt_maxsymlinklen
> 0)
721 newdir
.d_type
= IFTODT(ip
->i_mode
);
724 # if (BYTE_ORDER == LITTLE_ENDIAN)
725 { u_char tmp
= newdir
.d_namlen
;
726 newdir
.d_namlen
= newdir
.d_type
;
727 newdir
.d_type
= tmp
; }
730 return (ufs_direnter2(dvp
, &newdir
, cnp
->cn_cred
, cnp
->cn_proc
));
734 * Common entry point for directory entry removal used by ufs_direnter
737 ufs_direnter2(dvp
, dirp
, cr
, p
)
749 struct direct
*ep
, *nep
;
750 int error
, loc
, spacefree
;
753 struct mount
*mp
=dvp
->v_mount
;
754 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
755 #endif /* REV_ENDIAN_FS */
758 newentrysize
= DIRSIZ(FSFMT(dvp
), dirp
);
760 if (dp
->i_count
== 0) {
762 * If dp->i_count is 0, then namei could find no
763 * space in the directory. Here, dp->i_offset will
764 * be on a directory block boundary and we will write the
765 * new entry into a fresh block.
767 if (dp
->i_offset
& (DIRBLKSIZ
- 1))
768 panic("ufs_direnter2: newblk");
769 auio
.uio_offset
= dp
->i_offset
;
770 dirp
->d_reclen
= DIRBLKSIZ
;
771 auio
.uio_resid
= newentrysize
;
772 aiov
.iov_len
= newentrysize
;
773 aiov
.iov_base
= (caddr_t
)dirp
;
774 auio
.uio_iov
= &aiov
;
776 auio
.uio_rw
= UIO_WRITE
;
777 auio
.uio_segflg
= UIO_SYSSPACE
;
778 auio
.uio_procp
= (struct proc
*)0;
779 error
= VOP_WRITE(dvp
, &auio
, IO_SYNC
, cr
);
781 VFSTOUFS(dvp
->v_mount
)->um_mountp
->mnt_stat
.f_bsize
)
782 /* XXX should grow with balloc() */
783 panic("ufs_direnter2: frag size");
785 dp
->i_size
= roundup(dp
->i_size
, DIRBLKSIZ
);
786 dp
->i_flag
|= IN_CHANGE
;
792 * If dp->i_count is non-zero, then namei found space
793 * for the new entry in the range dp->i_offset to
794 * dp->i_offset + dp->i_count in the directory.
795 * To use this space, we may have to compact the entries located
796 * there, by copying them together towards the beginning of the
797 * block, leaving the free space in one usable chunk at the end.
801 * Increase size of directory if entry eats into new space.
802 * This should never push the size past a new multiple of
805 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
807 if (dp
->i_offset
+ dp
->i_count
> dp
->i_size
)
808 dp
->i_size
= dp
->i_offset
+ dp
->i_count
;
810 * Get the block containing the space for the new directory entry.
812 if (error
= VOP_BLKATOFF(dvp
, (off_t
)dp
->i_offset
, &dirbuf
, &bp
))
815 * Find space for the new entry. In the simple case, the entry at
816 * offset base will have the space. If it does not, then namei
817 * arranged that compacting the region dp->i_offset to
818 * dp->i_offset + dp->i_count would yield the
821 ep
= (struct direct
*)dirbuf
;
822 dsize
= DIRSIZ(FSFMT(dvp
), ep
);
823 spacefree
= ep
->d_reclen
- dsize
;
824 for (loc
= ep
->d_reclen
; loc
< dp
->i_count
; ) {
825 nep
= (struct direct
*)(dirbuf
+ loc
);
827 /* trim the existing slot */
828 ep
->d_reclen
= dsize
;
829 ep
= (struct direct
*)((char *)ep
+ dsize
);
831 /* overwrite; nothing there; header is ours */
834 dsize
= DIRSIZ(FSFMT(dvp
), nep
);
835 spacefree
+= nep
->d_reclen
- dsize
;
836 loc
+= nep
->d_reclen
;
837 bcopy((caddr_t
)nep
, (caddr_t
)ep
, dsize
);
840 * Update the pointer fields in the previous entry (if any),
841 * copy in the new entry, and write out the block.
843 if (ep
->d_ino
== 0 ||
844 (ep
->d_ino
== WINO
&&
845 bcmp(ep
->d_name
, dirp
->d_name
, dirp
->d_namlen
) == 0)) {
846 if (spacefree
+ dsize
< newentrysize
)
847 panic("ufs_direnter2: compact1");
848 dirp
->d_reclen
= spacefree
+ dsize
;
850 if (spacefree
< newentrysize
)
851 panic("ufs_direnter2: compact2");
852 dirp
->d_reclen
= spacefree
;
853 ep
->d_reclen
= dsize
;
854 ep
= (struct direct
*)((char *)ep
+ dsize
);
856 bcopy((caddr_t
)dirp
, (caddr_t
)ep
, (u_int
)newentrysize
);
859 byte_swap_dir_block_out(bp
);
860 #endif /* REV_ENDIAN_FS */
861 error
= VOP_BWRITE(bp
);
862 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
863 if (!error
&& dp
->i_endoff
&& dp
->i_endoff
< dp
->i_size
)
864 error
= VOP_TRUNCATE(dvp
, (off_t
)dp
->i_endoff
, IO_SYNC
, cr
, p
);
869 * Remove a directory entry after a call to namei, using
870 * the parameters which it left in nameidata. The entry
871 * dp->i_offset contains the offset into the directory of the
872 * entry to be eliminated. The dp->i_count field contains the
873 * size of the previous record in the directory. If this
874 * is 0, the first entry is being deleted, so we need only
875 * zero the inode number to mark the entry as free. If the
876 * entry is not the first in the directory, we must reclaim
877 * the space of the now empty record by adding the record size
878 * to the size of the previous entry.
881 ufs_dirremove(dvp
, cnp
)
883 struct componentname
*cnp
;
885 register struct inode
*dp
;
890 struct mount
*mp
=dvp
->v_mount
;
891 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
892 #endif /* REV_ENDIAN_FS */
896 if (cnp
->cn_flags
& DOWHITEOUT
) {
898 * Whiteout entry: set d_ino to WINO.
901 VOP_BLKATOFF(dvp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
))
907 byte_swap_dir_block_out(bp
);
908 #endif /* REV_ENDIAN_FS */
909 error
= VOP_BWRITE(bp
);
910 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
914 if (dp
->i_count
== 0) {
916 * First entry in block: set d_ino to zero.
919 VOP_BLKATOFF(dvp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
))
924 byte_swap_dir_block_out(bp
);
925 #endif /* REV_ENDIAN_FS */
926 error
= VOP_BWRITE(bp
);
927 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
931 * Collapse new free space into previous entry.
933 if (error
= VOP_BLKATOFF(dvp
, (off_t
)(dp
->i_offset
- dp
->i_count
),
936 ep
->d_reclen
+= dp
->i_reclen
;
939 byte_swap_dir_block_out(bp
);
940 #endif /* REV_ENDIAN_FS */
941 error
= VOP_BWRITE(bp
);
942 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
947 * Rewrite an existing directory entry to point at the inode
948 * supplied. The parameters describing the directory entry are
949 * set up by a call to namei.
952 ufs_dirrewrite(dp
, ip
, cnp
)
953 struct inode
*dp
, *ip
;
954 struct componentname
*cnp
;
958 struct vnode
*vdp
= ITOV(dp
);
961 if (error
= VOP_BLKATOFF(vdp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
))
963 ep
->d_ino
= ip
->i_number
;
964 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0)
965 ep
->d_type
= IFTODT(ip
->i_mode
);
967 if (vdp
->v_mount
->mnt_flag
& MNT_REVEND
)
968 byte_swap_dir_block_out(bp
);
969 #endif /* REV_ENDIAN_FS */
970 error
= VOP_BWRITE(bp
);
971 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
976 * Check if a directory is empty or not.
977 * Inode supplied must be locked.
979 * Using a struct dirtemplate here is not precisely
980 * what we want, but better than using a struct direct.
982 * NB: does not handle corrupted directories.
985 ufs_dirempty(ip
, parentino
, cred
)
986 register struct inode
*ip
;
991 struct dirtemplate dbuf
;
992 register struct direct
*dp
= (struct direct
*)&dbuf
;
993 int error
, count
, namlen
;
995 struct vnode
*vp
=ITOV(ip
);
996 struct mount
*mp
=vp
->v_mount
;
997 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
998 #endif /* REV_ENDIAN_FS */
1000 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1002 for (off
= 0; off
< ip
->i_size
; off
+= dp
->d_reclen
) {
1003 error
= vn_rdwr(UIO_READ
, ITOV(ip
), (caddr_t
)dp
, MINDIRSIZ
, off
,
1004 UIO_SYSSPACE
, IO_NODELOCKED
, cred
, &count
, (struct proc
*)0);
1006 * Since we read MINDIRSIZ, residual must
1007 * be 0 unless we're at end of file.
1009 if (error
|| count
!= 0)
1011 #if 0 /*REV_ENDIAN_FS */
1013 byte_swap_minidir_in(dp
);
1014 #endif /* REV_ENDIAN_FS */
1015 /* avoid infinite loops */
1016 if (dp
->d_reclen
== 0)
1018 /* skip empty entries */
1019 if (dp
->d_ino
== 0 || dp
->d_ino
== WINO
)
1021 /* accept only "." and ".." */
1022 # if (BYTE_ORDER == LITTLE_ENDIAN)
1023 if (ITOV(ip
)->v_mount
->mnt_maxsymlinklen
> 0)
1024 namlen
= dp
->d_namlen
;
1026 namlen
= dp
->d_type
;
1028 namlen
= dp
->d_namlen
;
1032 if (dp
->d_name
[0] != '.')
1035 * At this point namlen must be 1 or 2.
1036 * 1 implies ".", 2 implies ".." if second
1041 if (dp
->d_name
[1] == '.' && dp
->d_ino
== parentino
)
1049 * Check if source directory is in the path of the target directory.
1050 * Target is supplied locked, source is unlocked.
1051 * The target is always vput before returning.
1054 ufs_checkpath(source
, target
, cred
)
1055 struct inode
*source
, *target
;
1059 int error
, rootino
, namlen
;
1060 struct dirtemplate dirbuf
;
1063 if (target
->i_number
== source
->i_number
) {
1069 if (target
->i_number
== rootino
)
1073 if (vp
->v_type
!= VDIR
) {
1077 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&dirbuf
,
1078 sizeof (struct dirtemplate
), (off_t
)0, UIO_SYSSPACE
,
1079 IO_NODELOCKED
, cred
, (int *)0, (struct proc
*)0);
1082 # if (BYTE_ORDER == LITTLE_ENDIAN)
1083 if (vp
->v_mount
->mnt_maxsymlinklen
> 0)
1084 namlen
= dirbuf
.dotdot_namlen
;
1086 namlen
= dirbuf
.dotdot_type
;
1088 namlen
= dirbuf
.dotdot_namlen
;
1091 dirbuf
.dotdot_name
[0] != '.' ||
1092 dirbuf
.dotdot_name
[1] != '.') {
1096 if (dirbuf
.dotdot_ino
== source
->i_number
) {
1100 if (dirbuf
.dotdot_ino
== rootino
)
1103 if (error
= VFS_VGET(vp
->v_mount
, dirbuf
.dotdot_ino
, &vp
)) {
1110 if (error
== ENOTDIR
)
1111 printf("checkpath: .. not a directory\n");