]>
git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_lookup.c
1b7fc0cd037a8a774bef53dbe0e7a7df34bbe99e
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>
69 #include <sys/quota.h>
71 #include <ufs/ufs/quota.h>
72 #include <ufs/ufs/inode.h>
73 #include <ufs/ufs/dir.h>
74 #include <ufs/ufs/ufsmount.h>
75 #include <ufs/ufs/ufs_extern.h>
77 #include <ufs/ufs/ufs_byte_order.h>
78 #include <architecture/byte_order.h>
79 #endif /* REV_ENDIAN_FS */
81 extern struct nchstats nchstats
;
88 #define FSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
91 * Convert a component of a pathname into a pointer to a locked inode.
92 * This is a very central and rather complicated routine.
93 * If the file system is not maintained in a strict tree hierarchy,
94 * this can result in a deadlock situation (see comments in code below).
96 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
97 * on whether the name is to be looked up, created, renamed, or deleted.
98 * When CREATE, RENAME, or DELETE is specified, information usable in
99 * creating, renaming, or deleting a directory entry may be calculated.
100 * If flag has LOCKPARENT or'ed into it and the target of the pathname
101 * exists, lookup returns both the target and its parent directory locked.
102 * When creating or renaming and LOCKPARENT is specified, the target may
103 * not be ".". When deleting and LOCKPARENT is specified, the target may
104 * be "."., but the caller must check to ensure it does an vrele and vput
105 * instead of two vputs.
107 * Overall outline of ufs_lookup:
109 * check accessibility of directory
110 * look for name in cache, if found, then if at end of path
111 * and deleting or creating, drop it, else return name
112 * search for name in directory, to found or notfound
114 * if creating, return locked directory, leaving info on available slots
117 * if at end of path and deleting, return information to allow delete
118 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
119 * inode and return info to allow rewrite
120 * if not at end, add name to cache; if at end and neither creating
121 * nor deleting, add name to cache
125 struct vop_lookup_args
/* {
127 struct vnode **a_vpp;
128 struct componentname *a_cnp;
131 register struct vnode
*vdp
; /* vnode for directory being searched */
132 register struct inode
*dp
; /* inode for directory being searched */
133 struct buf
*bp
; /* a buffer of directory entries */
134 register struct direct
*ep
; /* the current directory entry */
135 int entryoffsetinblock
; /* offset of ep in bp's buffer */
136 enum {NONE
, COMPACT
, FOUND
} slotstatus
;
137 doff_t slotoffset
; /* offset of area with free space */
138 int slotsize
; /* size of area at slotoffset */
139 int slotfreespace
; /* amount of space free in slot */
140 int slotneeded
; /* size of the entry we're seeking */
141 int numdirpasses
; /* strategy for directory search */
142 doff_t endsearch
; /* offset to end directory search */
143 doff_t prevoff
; /* prev entry dp->i_offset */
144 struct vnode
*pdp
; /* saved dp during symlink work */
145 struct vnode
*tdp
; /* returned by VFS_VGET */
146 doff_t enduseful
; /* pointer past last used dir slot */
147 u_long bmask
; /* block offset mask */
148 int lockparent
; /* 1 => lockparent flag is set */
149 int wantparent
; /* 1 => wantparent or lockparent flag */
151 struct vnode
**vpp
= ap
->a_vpp
;
152 struct componentname
*cnp
= ap
->a_cnp
;
153 struct ucred
*cred
= cnp
->cn_cred
;
154 int flags
= cnp
->cn_flags
;
155 int nameiop
= cnp
->cn_nameiop
;
156 struct proc
*p
= cnp
->cn_proc
;
159 #endif /* REV_ENDIAN_FS */
167 lockparent
= flags
& LOCKPARENT
;
168 wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
170 rev_endian
=(vdp
->v_mount
->mnt_flag
& MNT_REVEND
);
171 #endif /* REV_ENDIAN_FS */
174 * Check accessiblity of directory.
176 if ((dp
->i_mode
& IFMT
) != IFDIR
)
178 if (error
= VOP_ACCESS(vdp
, VEXEC
, cred
, cnp
->cn_proc
))
180 if ((flags
& ISLASTCN
) && (vdp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
181 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
))
185 * We now have a segment name to search for, and a directory to search.
187 * Before tediously performing a linear scan of the directory,
188 * check the name cache to see if the directory/name pair
189 * we are looking for is known already.
191 if (error
= cache_lookup(vdp
, vpp
, cnp
)) {
192 int vpid
; /* capability number of vnode */
197 * Get the next vnode in the path.
198 * See comment below starting `Step through' for
199 * an explaination of the locking protocol.
205 if (pdp
== vdp
) { /* lookup on "." */
208 } else if (flags
& ISDOTDOT
) {
209 VOP_UNLOCK(pdp
, 0, p
);
210 error
= vget(vdp
, LK_EXCLUSIVE
, p
);
211 if (!error
&& lockparent
&& (flags
& ISLASTCN
))
212 error
= vn_lock(pdp
, LK_EXCLUSIVE
, p
);
214 error
= vget(vdp
, LK_EXCLUSIVE
, p
);
215 if (!lockparent
|| error
|| !(flags
& ISLASTCN
))
216 VOP_UNLOCK(pdp
, 0, p
);
219 * Check that the capability number did not change
220 * while we were waiting for the lock.
223 if (vpid
== vdp
->v_id
)
226 if (lockparent
&& pdp
!= vdp
&& (flags
& ISLASTCN
))
227 VOP_UNLOCK(pdp
, 0, p
);
229 if (error
= vn_lock(pdp
, LK_EXCLUSIVE
, p
))
237 * Suppress search for slots unless creating
238 * file and at end of pathname, in which case
239 * we watch for a place to put the new file in
240 * case it doesn't already exist.
243 slotfreespace
= slotsize
= slotneeded
= 0;
244 if ((nameiop
== CREATE
|| nameiop
== RENAME
) &&
245 (flags
& ISLASTCN
)) {
247 slotneeded
= (sizeof(struct direct
) - MAXNAMLEN
+
248 cnp
->cn_namelen
+ 3) &~ 3;
252 * If there is cached information on a previous search of
253 * this directory, pick up where we last left off.
254 * We cache only lookups as these are the most common
255 * and have the greatest payoff. Caching CREATE has little
256 * benefit as it usually must search the entire directory
257 * to determine that the entry does not exist. Caching the
258 * location of the last DELETE or RENAME has not reduced
259 * profiling time and hence has been removed in the interest
262 bmask
= VFSTOUFS(vdp
->v_mount
)->um_mountp
->mnt_stat
.f_iosize
- 1;
263 if (nameiop
!= LOOKUP
|| dp
->i_diroff
== 0 ||
264 dp
->i_diroff
> dp
->i_size
) {
265 entryoffsetinblock
= 0;
269 dp
->i_offset
= dp
->i_diroff
;
270 if ((entryoffsetinblock
= dp
->i_offset
& bmask
) &&
271 (error
= VOP_BLKATOFF(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
)))
274 nchstats
.ncs_2passes
++;
276 prevoff
= dp
->i_offset
;
277 endsearch
= roundup(dp
->i_size
, DIRBLKSIZ
);
281 while (dp
->i_offset
< endsearch
) {
283 * If necessary, get the next directory block.
285 if ((dp
->i_offset
& bmask
) == 0) {
289 byte_swap_dir_block_out(bp
);
290 #endif /* REV_ENDIAN_FS */
294 VOP_BLKATOFF(vdp
, (off_t
)dp
->i_offset
, NULL
, &bp
))
296 entryoffsetinblock
= 0;
299 * If still looking for a slot, and at a DIRBLKSIZE
300 * boundary, have to start looking for free space again.
302 if (slotstatus
== NONE
&&
303 (entryoffsetinblock
& (DIRBLKSIZ
- 1)) == 0) {
308 * Get pointer to next entry.
309 * Full validation checks are slow, so we only check
310 * enough to insure forward progress through the
311 * directory. Complete checks can be run by patching
312 * "dirchk" to be true.
314 ep
= (struct direct
*)((char *)bp
->b_data
+ entryoffsetinblock
);
315 if (ep
->d_reclen
== 0 ||
316 dirchk
&& ufs_dirbadentry(vdp
, ep
, entryoffsetinblock
)) {
319 ufs_dirbad(dp
, dp
->i_offset
, "mangled entry");
320 i
= DIRBLKSIZ
- (entryoffsetinblock
& (DIRBLKSIZ
- 1));
322 entryoffsetinblock
+= i
;
327 * If an appropriate sized slot has not yet been found,
328 * check to see if one is available. Also accumulate space
329 * in the current block so that we can determine if
330 * compaction is viable.
332 if (slotstatus
!= FOUND
) {
333 int size
= ep
->d_reclen
;
336 size
-= DIRSIZ(FSFMT(vdp
), ep
);
338 if (size
>= slotneeded
) {
340 slotoffset
= dp
->i_offset
;
341 slotsize
= ep
->d_reclen
;
342 } else if (slotstatus
== NONE
) {
343 slotfreespace
+= size
;
344 if (slotoffset
== -1)
345 slotoffset
= dp
->i_offset
;
346 if (slotfreespace
>= slotneeded
) {
347 slotstatus
= COMPACT
;
348 slotsize
= dp
->i_offset
+
349 ep
->d_reclen
- slotoffset
;
356 * Check for a name match.
359 # if (BYTE_ORDER == LITTLE_ENDIAN)
360 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0)
361 namlen
= ep
->d_namlen
;
365 namlen
= ep
->d_namlen
;
367 if (namlen
== cnp
->cn_namelen
&&
368 !bcmp(cnp
->cn_nameptr
, ep
->d_name
,
371 * Save directory entry's inode number and
372 * reclen in ndp->ni_ufs area, and release
375 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0 &&
376 ep
->d_type
== DT_WHT
) {
378 slotoffset
= dp
->i_offset
;
379 slotsize
= ep
->d_reclen
;
380 dp
->i_reclen
= slotsize
;
381 enduseful
= dp
->i_size
;
382 ap
->a_cnp
->cn_flags
|= ISWHITEOUT
;
386 dp
->i_ino
= ep
->d_ino
;
387 dp
->i_reclen
= ep
->d_reclen
;
390 byte_swap_dir_block_out(bp
);
391 #endif /* REV_ENDIAN_FS */
396 prevoff
= dp
->i_offset
;
397 dp
->i_offset
+= ep
->d_reclen
;
398 entryoffsetinblock
+= ep
->d_reclen
;
400 enduseful
= dp
->i_offset
;
404 * If we started in the middle of the directory and failed
405 * to find our target, we must check the beginning as well.
407 if (numdirpasses
== 2) {
410 endsearch
= dp
->i_diroff
;
416 byte_swap_dir_block_out(bp
);
417 #endif /* REV_ENDIAN_FS */
421 * If creating, and at end of pathname and current
422 * directory has not been removed, then can consider
423 * allowing file to be created.
425 if ((nameiop
== CREATE
|| nameiop
== RENAME
||
426 (nameiop
== DELETE
&&
427 (ap
->a_cnp
->cn_flags
& DOWHITEOUT
) &&
428 (ap
->a_cnp
->cn_flags
& ISWHITEOUT
))) &&
429 (flags
& ISLASTCN
) && dp
->i_nlink
!= 0) {
431 * Access for write is interpreted as allowing
432 * creation of files in the directory.
434 if (error
= VOP_ACCESS(vdp
, VWRITE
, cred
, cnp
->cn_proc
))
437 * Return an indication of where the new directory
438 * entry should be put. If we didn't find a slot,
439 * then set dp->i_count to 0 indicating
440 * that the new slot belongs at the end of the
441 * directory. If we found a slot, then the new entry
442 * can be put in the range from dp->i_offset to
443 * dp->i_offset + dp->i_count.
445 if (slotstatus
== NONE
) {
446 dp
->i_offset
= roundup(dp
->i_size
, DIRBLKSIZ
);
448 enduseful
= dp
->i_offset
;
449 } else if (nameiop
== DELETE
) {
450 dp
->i_offset
= slotoffset
;
451 if ((dp
->i_offset
& (DIRBLKSIZ
- 1)) == 0)
454 dp
->i_count
= dp
->i_offset
- prevoff
;
456 dp
->i_offset
= slotoffset
;
457 dp
->i_count
= slotsize
;
458 if (enduseful
< slotoffset
+ slotsize
)
459 enduseful
= slotoffset
+ slotsize
;
461 dp
->i_endoff
= roundup(enduseful
, DIRBLKSIZ
);
462 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
464 * We return with the directory locked, so that
465 * the parameters we set up above will still be
466 * valid if we actually decide to do a direnter().
467 * We return ni_vp == NULL to indicate that the entry
468 * does not currently exist; we leave a pointer to
469 * the (locked) directory inode in ndp->ni_dvp.
470 * The pathname buffer is saved so that the name
471 * can be obtained later.
473 * NB - if the directory is unlocked, then this
474 * information cannot be used.
476 cnp
->cn_flags
|= SAVENAME
;
478 VOP_UNLOCK(vdp
, 0, p
);
479 return (EJUSTRETURN
);
482 * Insert name into cache (as non-existent) if appropriate.
484 if ((cnp
->cn_flags
& MAKEENTRY
) && nameiop
!= CREATE
)
485 cache_enter(vdp
, *vpp
, cnp
);
489 if (numdirpasses
== 2)
490 nchstats
.ncs_pass2
++;
492 * Check that directory length properly reflects presence
495 if (entryoffsetinblock
+ DIRSIZ(FSFMT(vdp
), ep
) > dp
->i_size
) {
496 ufs_dirbad(dp
, dp
->i_offset
, "i_size too small");
497 dp
->i_size
= entryoffsetinblock
+ DIRSIZ(FSFMT(vdp
), ep
);
498 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
502 * Found component in pathname.
503 * If the final component of path name, save information
504 * in the cache as to where the entry was found.
506 if ((flags
& ISLASTCN
) && nameiop
== LOOKUP
)
507 dp
->i_diroff
= dp
->i_offset
&~ (DIRBLKSIZ
- 1);
510 * If deleting, and at end of pathname, return
511 * parameters which can be used to remove file.
512 * If the wantparent flag isn't set, we return only
513 * the directory (in ndp->ni_dvp), otherwise we go
514 * on and lock the inode, being careful with ".".
516 if (nameiop
== DELETE
&& (flags
& ISLASTCN
)) {
518 * Write access to directory required to delete files.
520 if (error
= VOP_ACCESS(vdp
, VWRITE
, cred
, cnp
->cn_proc
))
523 * Return pointer to current entry in dp->i_offset,
524 * and distance past previous entry (if there
525 * is a previous entry in this block) in dp->i_count.
526 * Save directory inode pointer in ndp->ni_dvp for dirremove().
528 if ((dp
->i_offset
& (DIRBLKSIZ
- 1)) == 0)
531 dp
->i_count
= dp
->i_offset
- prevoff
;
532 if (dp
->i_number
== dp
->i_ino
) {
537 if (error
= VFS_VGET(vdp
->v_mount
, dp
->i_ino
, &tdp
))
540 * If directory is "sticky", then user must own
541 * the directory, or the file in it, else she
542 * may not delete it (unless she's root). This
543 * implements append-only directories.
545 if ((dp
->i_mode
& ISVTX
) &&
547 cred
->cr_uid
!= dp
->i_uid
&&
548 tdp
->v_type
!= VLNK
&&
549 VTOI(tdp
)->i_uid
!= cred
->cr_uid
) {
555 VOP_UNLOCK(vdp
, 0, p
);
560 * If rewriting (RENAME), return the inode and the
561 * information required to rewrite the present directory
562 * Must get inode of directory entry to verify it's a
563 * regular file, or empty directory.
565 if (nameiop
== RENAME
&& wantparent
&& (flags
& ISLASTCN
)) {
566 if (error
= VOP_ACCESS(vdp
, VWRITE
, cred
, cnp
->cn_proc
))
569 * Careful about locking second inode.
570 * This can only occur if the target is ".".
572 if (dp
->i_number
== dp
->i_ino
)
574 if (error
= VFS_VGET(vdp
->v_mount
, dp
->i_ino
, &tdp
))
577 cnp
->cn_flags
|= SAVENAME
;
579 VOP_UNLOCK(vdp
, 0, p
);
584 * Step through the translation in the name. We do not `vput' the
585 * directory because we may need it again if a symbolic link
586 * is relative to the current directory. Instead we save it
587 * unlocked as "pdp". We must get the target inode before unlocking
588 * the directory to insure that the inode will not be removed
589 * before we get it. We prevent deadlock by always fetching
590 * inodes from the root, moving down the directory tree. Thus
591 * when following backward pointers ".." we must unlock the
592 * parent directory before getting the requested directory.
593 * There is a potential race condition here if both the current
594 * and parent directories are removed before the VFS_VGET for the
595 * inode associated with ".." returns. We hope that this occurs
596 * infrequently since we cannot avoid this race condition without
597 * implementing a sophisticated deadlock detection algorithm.
598 * Note also that this simple deadlock detection scheme will not
599 * work if the file system has any hard links other than ".."
600 * that point backwards in the directory structure.
603 if (flags
& ISDOTDOT
) {
604 VOP_UNLOCK(pdp
, 0, p
); /* race to get the inode */
605 if (error
= VFS_VGET(vdp
->v_mount
, dp
->i_ino
, &tdp
)) {
606 vn_lock(pdp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
609 if (lockparent
&& (flags
& ISLASTCN
) &&
610 (error
= vn_lock(pdp
, LK_EXCLUSIVE
, p
))) {
615 } else if (dp
->i_number
== dp
->i_ino
) {
616 VREF(vdp
); /* we want ourself, ie "." */
619 if (error
= VFS_VGET(vdp
->v_mount
, dp
->i_ino
, &tdp
))
621 if (!lockparent
|| !(flags
& ISLASTCN
))
622 VOP_UNLOCK(pdp
, 0, p
);
627 * Insert name into cache if appropriate.
629 if (cnp
->cn_flags
& MAKEENTRY
)
630 cache_enter(vdp
, *vpp
, cnp
);
635 ufs_dirbad(ip
, offset
, how
)
642 mp
= ITOV(ip
)->v_mount
;
643 (void)printf("%s: bad dir ino %d at offset %d: %s\n",
644 mp
->mnt_stat
.f_mntonname
, ip
->i_number
, offset
, how
);
645 if ((mp
->mnt_stat
.f_flags
& MNT_RDONLY
) == 0)
650 * Do consistency checking on a directory entry:
651 * record length must be multiple of 4
652 * entry must fit in rest of its DIRBLKSIZ block
653 * record must be large enough to contain entry
654 * name is not longer than MAXNAMLEN
655 * name must be as long as advertised, and null terminated
658 ufs_dirbadentry(dp
, ep
, entryoffsetinblock
)
660 register struct direct
*ep
;
661 int entryoffsetinblock
;
666 # if (BYTE_ORDER == LITTLE_ENDIAN)
667 if (dp
->v_mount
->mnt_maxsymlinklen
> 0)
668 namlen
= ep
->d_namlen
;
672 namlen
= ep
->d_namlen
;
674 if ((ep
->d_reclen
& 0x3) != 0 ||
675 ep
->d_reclen
> DIRBLKSIZ
- (entryoffsetinblock
& (DIRBLKSIZ
- 1)) ||
676 ep
->d_reclen
< DIRSIZ(FSFMT(dp
), ep
) || namlen
> MAXNAMLEN
) {
678 printf("First bad\n");
683 for (i
= 0; i
< namlen
; i
++)
684 if (ep
->d_name
[i
] == '\0') {
686 printf("Second bad\n");
697 * Write a directory entry after a call to namei, using the parameters
698 * that it left in nameidata. The argument ip is the inode which the new
699 * directory entry will refer to. Dvp is a pointer to the directory to
700 * be written, which was left locked by namei. Remaining parameters
701 * (dp->i_offset, dp->i_count) indicate how the space for the new
702 * entry is to be obtained.
705 ufs_direnter(ip
, dvp
, cnp
)
708 register struct componentname
*cnp
;
710 register struct inode
*dp
;
711 struct direct newdir
;
714 if ((cnp
->cn_flags
& SAVENAME
) == 0)
715 panic("direnter: missing name");
718 newdir
.d_ino
= ip
->i_number
;
719 newdir
.d_namlen
= cnp
->cn_namelen
;
720 bcopy(cnp
->cn_nameptr
, newdir
.d_name
, (unsigned)cnp
->cn_namelen
+ 1);
721 if (dvp
->v_mount
->mnt_maxsymlinklen
> 0)
722 newdir
.d_type
= IFTODT(ip
->i_mode
);
725 # if (BYTE_ORDER == LITTLE_ENDIAN)
726 { u_char tmp
= newdir
.d_namlen
;
727 newdir
.d_namlen
= newdir
.d_type
;
728 newdir
.d_type
= tmp
; }
731 return (ufs_direnter2(dvp
, &newdir
, cnp
->cn_cred
, cnp
->cn_proc
));
735 * Common entry point for directory entry removal used by ufs_direnter
738 ufs_direnter2(dvp
, dirp
, cr
, p
)
750 struct direct
*ep
, *nep
;
751 int error
, loc
, spacefree
;
754 struct mount
*mp
=dvp
->v_mount
;
755 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
756 #endif /* REV_ENDIAN_FS */
759 newentrysize
= DIRSIZ(FSFMT(dvp
), dirp
);
761 if (dp
->i_count
== 0) {
763 * If dp->i_count is 0, then namei could find no
764 * space in the directory. Here, dp->i_offset will
765 * be on a directory block boundary and we will write the
766 * new entry into a fresh block.
768 if (dp
->i_offset
& (DIRBLKSIZ
- 1))
769 panic("ufs_direnter2: newblk");
770 auio
.uio_offset
= dp
->i_offset
;
771 dirp
->d_reclen
= DIRBLKSIZ
;
772 auio
.uio_resid
= newentrysize
;
773 aiov
.iov_len
= newentrysize
;
774 aiov
.iov_base
= (caddr_t
)dirp
;
775 auio
.uio_iov
= &aiov
;
777 auio
.uio_rw
= UIO_WRITE
;
778 auio
.uio_segflg
= UIO_SYSSPACE
;
779 auio
.uio_procp
= (struct proc
*)0;
780 error
= VOP_WRITE(dvp
, &auio
, IO_SYNC
, cr
);
782 VFSTOUFS(dvp
->v_mount
)->um_mountp
->mnt_stat
.f_bsize
)
783 /* XXX should grow with balloc() */
784 panic("ufs_direnter2: frag size");
786 dp
->i_size
= roundup(dp
->i_size
, DIRBLKSIZ
);
787 dp
->i_flag
|= IN_CHANGE
;
793 * If dp->i_count is non-zero, then namei found space
794 * for the new entry in the range dp->i_offset to
795 * dp->i_offset + dp->i_count in the directory.
796 * To use this space, we may have to compact the entries located
797 * there, by copying them together towards the beginning of the
798 * block, leaving the free space in one usable chunk at the end.
802 * Increase size of directory if entry eats into new space.
803 * This should never push the size past a new multiple of
806 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
808 if (dp
->i_offset
+ dp
->i_count
> dp
->i_size
)
809 dp
->i_size
= dp
->i_offset
+ dp
->i_count
;
811 * Get the block containing the space for the new directory entry.
813 if (error
= VOP_BLKATOFF(dvp
, (off_t
)dp
->i_offset
, &dirbuf
, &bp
))
816 * Find space for the new entry. In the simple case, the entry at
817 * offset base will have the space. If it does not, then namei
818 * arranged that compacting the region dp->i_offset to
819 * dp->i_offset + dp->i_count would yield the
822 ep
= (struct direct
*)dirbuf
;
823 dsize
= DIRSIZ(FSFMT(dvp
), ep
);
824 spacefree
= ep
->d_reclen
- dsize
;
825 for (loc
= ep
->d_reclen
; loc
< dp
->i_count
; ) {
826 nep
= (struct direct
*)(dirbuf
+ loc
);
828 /* trim the existing slot */
829 ep
->d_reclen
= dsize
;
830 ep
= (struct direct
*)((char *)ep
+ dsize
);
832 /* overwrite; nothing there; header is ours */
835 dsize
= DIRSIZ(FSFMT(dvp
), nep
);
836 spacefree
+= nep
->d_reclen
- dsize
;
837 loc
+= nep
->d_reclen
;
838 bcopy((caddr_t
)nep
, (caddr_t
)ep
, dsize
);
841 * Update the pointer fields in the previous entry (if any),
842 * copy in the new entry, and write out the block.
844 if (ep
->d_ino
== 0 ||
845 (ep
->d_ino
== WINO
&&
846 bcmp(ep
->d_name
, dirp
->d_name
, dirp
->d_namlen
) == 0)) {
847 if (spacefree
+ dsize
< newentrysize
)
848 panic("ufs_direnter2: compact1");
849 dirp
->d_reclen
= spacefree
+ dsize
;
851 if (spacefree
< newentrysize
)
852 panic("ufs_direnter2: compact2");
853 dirp
->d_reclen
= spacefree
;
854 ep
->d_reclen
= dsize
;
855 ep
= (struct direct
*)((char *)ep
+ dsize
);
857 bcopy((caddr_t
)dirp
, (caddr_t
)ep
, (u_int
)newentrysize
);
860 byte_swap_dir_block_out(bp
);
861 #endif /* REV_ENDIAN_FS */
862 error
= VOP_BWRITE(bp
);
863 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
864 if (!error
&& dp
->i_endoff
&& dp
->i_endoff
< dp
->i_size
)
865 error
= VOP_TRUNCATE(dvp
, (off_t
)dp
->i_endoff
, IO_SYNC
, cr
, p
);
870 * Remove a directory entry after a call to namei, using
871 * the parameters which it left in nameidata. The entry
872 * dp->i_offset contains the offset into the directory of the
873 * entry to be eliminated. The dp->i_count field contains the
874 * size of the previous record in the directory. If this
875 * is 0, the first entry is being deleted, so we need only
876 * zero the inode number to mark the entry as free. If the
877 * entry is not the first in the directory, we must reclaim
878 * the space of the now empty record by adding the record size
879 * to the size of the previous entry.
882 ufs_dirremove(dvp
, cnp
)
884 struct componentname
*cnp
;
886 register struct inode
*dp
;
891 struct mount
*mp
=dvp
->v_mount
;
892 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
893 #endif /* REV_ENDIAN_FS */
897 if (cnp
->cn_flags
& DOWHITEOUT
) {
899 * Whiteout entry: set d_ino to WINO.
902 VOP_BLKATOFF(dvp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
))
908 byte_swap_dir_block_out(bp
);
909 #endif /* REV_ENDIAN_FS */
910 error
= VOP_BWRITE(bp
);
911 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
915 if (dp
->i_count
== 0) {
917 * First entry in block: set d_ino to zero.
920 VOP_BLKATOFF(dvp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
))
925 byte_swap_dir_block_out(bp
);
926 #endif /* REV_ENDIAN_FS */
927 error
= VOP_BWRITE(bp
);
928 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
932 * Collapse new free space into previous entry.
934 if (error
= VOP_BLKATOFF(dvp
, (off_t
)(dp
->i_offset
- dp
->i_count
),
937 ep
->d_reclen
+= dp
->i_reclen
;
940 byte_swap_dir_block_out(bp
);
941 #endif /* REV_ENDIAN_FS */
942 error
= VOP_BWRITE(bp
);
943 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
948 * Rewrite an existing directory entry to point at the inode
949 * supplied. The parameters describing the directory entry are
950 * set up by a call to namei.
953 ufs_dirrewrite(dp
, ip
, cnp
)
954 struct inode
*dp
, *ip
;
955 struct componentname
*cnp
;
959 struct vnode
*vdp
= ITOV(dp
);
962 if (error
= VOP_BLKATOFF(vdp
, (off_t
)dp
->i_offset
, (char **)&ep
, &bp
))
964 ep
->d_ino
= ip
->i_number
;
965 if (vdp
->v_mount
->mnt_maxsymlinklen
> 0)
966 ep
->d_type
= IFTODT(ip
->i_mode
);
968 if (vdp
->v_mount
->mnt_flag
& MNT_REVEND
)
969 byte_swap_dir_block_out(bp
);
970 #endif /* REV_ENDIAN_FS */
971 error
= VOP_BWRITE(bp
);
972 dp
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
977 * Check if a directory is empty or not.
978 * Inode supplied must be locked.
980 * Using a struct dirtemplate here is not precisely
981 * what we want, but better than using a struct direct.
983 * NB: does not handle corrupted directories.
986 ufs_dirempty(ip
, parentino
, cred
)
987 register struct inode
*ip
;
992 struct dirtemplate dbuf
;
993 register struct direct
*dp
= (struct direct
*)&dbuf
;
994 int error
, count
, namlen
;
996 struct vnode
*vp
=ITOV(ip
);
997 struct mount
*mp
=vp
->v_mount
;
998 int rev_endian
=(mp
->mnt_flag
& MNT_REVEND
);
999 #endif /* REV_ENDIAN_FS */
1001 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1003 for (off
= 0; off
< ip
->i_size
; off
+= dp
->d_reclen
) {
1004 error
= vn_rdwr(UIO_READ
, ITOV(ip
), (caddr_t
)dp
, MINDIRSIZ
, off
,
1005 UIO_SYSSPACE
, IO_NODELOCKED
, cred
, &count
, (struct proc
*)0);
1007 * Since we read MINDIRSIZ, residual must
1008 * be 0 unless we're at end of file.
1010 if (error
|| count
!= 0)
1012 #if 0 /*REV_ENDIAN_FS */
1014 byte_swap_minidir_in(dp
);
1015 #endif /* REV_ENDIAN_FS */
1016 /* avoid infinite loops */
1017 if (dp
->d_reclen
== 0)
1019 /* skip empty entries */
1020 if (dp
->d_ino
== 0 || dp
->d_ino
== WINO
)
1022 /* accept only "." and ".." */
1023 # if (BYTE_ORDER == LITTLE_ENDIAN)
1024 if (ITOV(ip
)->v_mount
->mnt_maxsymlinklen
> 0)
1025 namlen
= dp
->d_namlen
;
1027 namlen
= dp
->d_type
;
1029 namlen
= dp
->d_namlen
;
1033 if (dp
->d_name
[0] != '.')
1036 * At this point namlen must be 1 or 2.
1037 * 1 implies ".", 2 implies ".." if second
1042 if (dp
->d_name
[1] == '.' && dp
->d_ino
== parentino
)
1050 * Check if source directory is in the path of the target directory.
1051 * Target is supplied locked, source is unlocked.
1052 * The target is always vput before returning.
1055 ufs_checkpath(source
, target
, cred
)
1056 struct inode
*source
, *target
;
1060 int error
, rootino
, namlen
;
1061 struct dirtemplate dirbuf
;
1064 if (target
->i_number
== source
->i_number
) {
1070 if (target
->i_number
== rootino
)
1074 if (vp
->v_type
!= VDIR
) {
1078 error
= vn_rdwr(UIO_READ
, vp
, (caddr_t
)&dirbuf
,
1079 sizeof (struct dirtemplate
), (off_t
)0, UIO_SYSSPACE
,
1080 IO_NODELOCKED
, cred
, (int *)0, (struct proc
*)0);
1083 # if (BYTE_ORDER == LITTLE_ENDIAN)
1084 if (vp
->v_mount
->mnt_maxsymlinklen
> 0)
1085 namlen
= dirbuf
.dotdot_namlen
;
1087 namlen
= dirbuf
.dotdot_type
;
1089 namlen
= dirbuf
.dotdot_namlen
;
1092 dirbuf
.dotdot_name
[0] != '.' ||
1093 dirbuf
.dotdot_name
[1] != '.') {
1097 if (dirbuf
.dotdot_ino
== source
->i_number
) {
1101 if (dirbuf
.dotdot_ino
== rootino
)
1104 if (error
= VFS_VGET(vp
->v_mount
, dirbuf
.dotdot_ino
, &vp
)) {
1111 if (error
== ENOTDIR
)
1112 printf("checkpath: .. not a directory\n");