]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_lookup.c
a2583678bc549fef47bdec991ace0e09fd0a94e8
[apple/xnu.git] / bsd / ufs / ufs / ufs_lookup.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
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.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 *
60 * @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
61 */
62 #include <rev_endian_fs.h>
63 #include <sys/param.h>
64 #include <sys/namei.h>
65 #include <sys/buf.h>
66 #include <sys/file.h>
67 #include <sys/mount.h>
68 #include <sys/vnode.h>
69
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>
75 #if REV_ENDIAN_FS
76 #include <ufs/ufs/ufs_byte_order.h>
77 #include <architecture/byte_order.h>
78 #endif /* REV_ENDIAN_FS */
79
80 extern struct nchstats nchstats;
81 #if DIAGNOSTIC
82 int dirchk = 1;
83 #else
84 int dirchk = 0;
85 #endif
86
87 #define FSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
88
89 /*
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).
94 *
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.
105 *
106 * Overall outline of ufs_lookup:
107 *
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
112 * notfound:
113 * if creating, return locked directory, leaving info on available slots
114 * else return error
115 * found:
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
121 */
122 int
123 ufs_lookup(ap)
124 struct vop_lookup_args /* {
125 struct vnode *a_dvp;
126 struct vnode **a_vpp;
127 struct componentname *a_cnp;
128 } */ *ap;
129 {
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 */
149 int namlen, error;
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;
156 #if REV_ENDIAN_FS
157 int rev_endian=0;
158 #endif /* REV_ENDIAN_FS */
159
160
161 bp = NULL;
162 slotoffset = -1;
163 *vpp = NULL;
164 vdp = ap->a_dvp;
165 dp = VTOI(vdp);
166 lockparent = flags & LOCKPARENT;
167 wantparent = flags & (LOCKPARENT|WANTPARENT);
168 #if REV_ENDIAN_FS
169 rev_endian=(vdp->v_mount->mnt_flag & MNT_REVEND);
170 #endif /* REV_ENDIAN_FS */
171
172 /*
173 * Check accessiblity of directory.
174 */
175 if ((dp->i_mode & IFMT) != IFDIR)
176 return (ENOTDIR);
177 if (error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc))
178 return (error);
179 if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
180 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
181 return (EROFS);
182
183 /*
184 * We now have a segment name to search for, and a directory to search.
185 *
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.
189 */
190 if (error = cache_lookup(vdp, vpp, cnp)) {
191 int vpid; /* capability number of vnode */
192
193 if (error == ENOENT)
194 return (error);
195 /*
196 * Get the next vnode in the path.
197 * See comment below starting `Step through' for
198 * an explaination of the locking protocol.
199 */
200 pdp = vdp;
201 dp = VTOI(*vpp);
202 vdp = *vpp;
203 vpid = vdp->v_id;
204 if (pdp == vdp) { /* lookup on "." */
205 VREF(vdp);
206 error = 0;
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);
212 } else {
213 error = vget(vdp, LK_EXCLUSIVE, p);
214 if (!lockparent || error || !(flags & ISLASTCN))
215 VOP_UNLOCK(pdp, 0, p);
216 }
217 /*
218 * Check that the capability number did not change
219 * while we were waiting for the lock.
220 */
221 if (!error) {
222 if (vpid == vdp->v_id)
223 return (0);
224 vput(vdp);
225 if (lockparent && pdp != vdp && (flags & ISLASTCN))
226 VOP_UNLOCK(pdp, 0, p);
227 }
228 if (error = vn_lock(pdp, LK_EXCLUSIVE, p))
229 return (error);
230 vdp = pdp;
231 dp = VTOI(pdp);
232 *vpp = NULL;
233 }
234
235 /*
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.
240 */
241 slotstatus = FOUND;
242 slotfreespace = slotsize = slotneeded = 0;
243 if ((nameiop == CREATE || nameiop == RENAME) &&
244 (flags & ISLASTCN)) {
245 slotstatus = NONE;
246 slotneeded = (sizeof(struct direct) - MAXNAMLEN +
247 cnp->cn_namelen + 3) &~ 3;
248 }
249
250 /*
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
259 * of simplicity.
260 */
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;
265 dp->i_offset = 0;
266 numdirpasses = 1;
267 } else {
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)))
271 return (error);
272 numdirpasses = 2;
273 nchstats.ncs_2passes++;
274 }
275 prevoff = dp->i_offset;
276 endsearch = roundup(dp->i_size, DIRBLKSIZ);
277 enduseful = 0;
278
279 searchloop:
280 while (dp->i_offset < endsearch) {
281 /*
282 * If necessary, get the next directory block.
283 */
284 if ((dp->i_offset & bmask) == 0) {
285 if (bp != NULL) {
286 #if REV_ENDIAN_FS
287 if (rev_endian)
288 byte_swap_dir_block_out(bp);
289 #endif /* REV_ENDIAN_FS */
290 brelse(bp);
291 }
292 if (error =
293 VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp))
294 return (error);
295 entryoffsetinblock = 0;
296 }
297 /*
298 * If still looking for a slot, and at a DIRBLKSIZE
299 * boundary, have to start looking for free space again.
300 */
301 if (slotstatus == NONE &&
302 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
303 slotoffset = -1;
304 slotfreespace = 0;
305 }
306 /*
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.
312 */
313 ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
314 if (ep->d_reclen == 0 ||
315 dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock)) {
316 int i;
317
318 ufs_dirbad(dp, dp->i_offset, "mangled entry");
319 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
320 dp->i_offset += i;
321 entryoffsetinblock += i;
322 continue;
323 }
324
325 /*
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.
330 */
331 if (slotstatus != FOUND) {
332 int size = ep->d_reclen;
333
334 if (ep->d_ino != 0)
335 size -= DIRSIZ(FSFMT(vdp), ep);
336 if (size > 0) {
337 if (size >= slotneeded) {
338 slotstatus = FOUND;
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;
349 }
350 }
351 }
352 }
353
354 /*
355 * Check for a name match.
356 */
357 if (ep->d_ino) {
358 # if (BYTE_ORDER == LITTLE_ENDIAN)
359 if (vdp->v_mount->mnt_maxsymlinklen > 0)
360 namlen = ep->d_namlen;
361 else
362 namlen = ep->d_type;
363 # else
364 namlen = ep->d_namlen;
365 # endif
366 if (namlen == cnp->cn_namelen &&
367 !bcmp(cnp->cn_nameptr, ep->d_name,
368 (unsigned)namlen)) {
369 /*
370 * Save directory entry's inode number and
371 * reclen in ndp->ni_ufs area, and release
372 * directory buffer.
373 */
374 if (vdp->v_mount->mnt_maxsymlinklen > 0 &&
375 ep->d_type == DT_WHT) {
376 slotstatus = FOUND;
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;
382 numdirpasses--;
383 goto notfound;
384 }
385 dp->i_ino = ep->d_ino;
386 dp->i_reclen = ep->d_reclen;
387 #if REV_ENDIAN_FS
388 if (rev_endian)
389 byte_swap_dir_block_out(bp);
390 #endif /* REV_ENDIAN_FS */
391 brelse(bp);
392 goto found;
393 }
394 }
395 prevoff = dp->i_offset;
396 dp->i_offset += ep->d_reclen;
397 entryoffsetinblock += ep->d_reclen;
398 if (ep->d_ino)
399 enduseful = dp->i_offset;
400 }
401 notfound:
402 /*
403 * If we started in the middle of the directory and failed
404 * to find our target, we must check the beginning as well.
405 */
406 if (numdirpasses == 2) {
407 numdirpasses--;
408 dp->i_offset = 0;
409 endsearch = dp->i_diroff;
410 goto searchloop;
411 }
412 if (bp != NULL) {
413 #if REV_ENDIAN_FS
414 if (rev_endian)
415 byte_swap_dir_block_out(bp);
416 #endif /* REV_ENDIAN_FS */
417 brelse(bp);
418 }
419 /*
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.
423 */
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) {
429 /*
430 * Access for write is interpreted as allowing
431 * creation of files in the directory.
432 */
433 if (error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc))
434 return (error);
435 /*
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.
443 */
444 if (slotstatus == NONE) {
445 dp->i_offset = roundup(dp->i_size, DIRBLKSIZ);
446 dp->i_count = 0;
447 enduseful = dp->i_offset;
448 } else if (nameiop == DELETE) {
449 dp->i_offset = slotoffset;
450 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
451 dp->i_count = 0;
452 else
453 dp->i_count = dp->i_offset - prevoff;
454 } else {
455 dp->i_offset = slotoffset;
456 dp->i_count = slotsize;
457 if (enduseful < slotoffset + slotsize)
458 enduseful = slotoffset + slotsize;
459 }
460 dp->i_endoff = roundup(enduseful, DIRBLKSIZ);
461 dp->i_flag |= IN_CHANGE | IN_UPDATE;
462 /*
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.
471 *
472 * NB - if the directory is unlocked, then this
473 * information cannot be used.
474 */
475 cnp->cn_flags |= SAVENAME;
476 if (!lockparent)
477 VOP_UNLOCK(vdp, 0, p);
478 return (EJUSTRETURN);
479 }
480 /*
481 * Insert name into cache (as non-existent) if appropriate.
482 */
483 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
484 cache_enter(vdp, *vpp, cnp);
485 return (ENOENT);
486
487 found:
488 if (numdirpasses == 2)
489 nchstats.ncs_pass2++;
490 /*
491 * Check that directory length properly reflects presence
492 * of this entry.
493 */
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;
498 }
499
500 /*
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.
504 */
505 if ((flags & ISLASTCN) && nameiop == LOOKUP)
506 dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
507
508 /*
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 ".".
514 */
515 if (nameiop == DELETE && (flags & ISLASTCN)) {
516 /*
517 * Write access to directory required to delete files.
518 */
519 if (error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc))
520 return (error);
521 /*
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().
526 */
527 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
528 dp->i_count = 0;
529 else
530 dp->i_count = dp->i_offset - prevoff;
531 if (dp->i_number == dp->i_ino) {
532 VREF(vdp);
533 *vpp = vdp;
534 return (0);
535 }
536 if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp))
537 return (error);
538 /*
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.
543 */
544 if ((dp->i_mode & ISVTX) &&
545 cred->cr_uid != 0 &&
546 cred->cr_uid != dp->i_uid &&
547 tdp->v_type != VLNK &&
548 VTOI(tdp)->i_uid != cred->cr_uid) {
549 vput(tdp);
550 return (EPERM);
551 }
552 *vpp = tdp;
553 if (!lockparent)
554 VOP_UNLOCK(vdp, 0, p);
555 return (0);
556 }
557
558 /*
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.
563 */
564 if (nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
565 if (error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc))
566 return (error);
567 /*
568 * Careful about locking second inode.
569 * This can only occur if the target is ".".
570 */
571 if (dp->i_number == dp->i_ino)
572 return (EISDIR);
573 if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp))
574 return (error);
575 *vpp = tdp;
576 cnp->cn_flags |= SAVENAME;
577 if (!lockparent)
578 VOP_UNLOCK(vdp, 0, p);
579 return (0);
580 }
581
582 /*
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.
600 */
601 pdp = vdp;
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);
606 return (error);
607 }
608 if (lockparent && (flags & ISLASTCN) &&
609 (error = vn_lock(pdp, LK_EXCLUSIVE, p))) {
610 vput(tdp);
611 return (error);
612 }
613 *vpp = tdp;
614 } else if (dp->i_number == dp->i_ino) {
615 VREF(vdp); /* we want ourself, ie "." */
616 *vpp = vdp;
617 } else {
618 if (error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp))
619 return (error);
620 if (!lockparent || !(flags & ISLASTCN))
621 VOP_UNLOCK(pdp, 0, p);
622 *vpp = tdp;
623 }
624
625 /*
626 * Insert name into cache if appropriate.
627 */
628 if (cnp->cn_flags & MAKEENTRY)
629 cache_enter(vdp, *vpp, cnp);
630 return (0);
631 }
632
633 void
634 ufs_dirbad(ip, offset, how)
635 struct inode *ip;
636 doff_t offset;
637 char *how;
638 {
639 struct mount *mp;
640
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)
645 panic("bad dir");
646 }
647
648 /*
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
655 */
656 int
657 ufs_dirbadentry(dp, ep, entryoffsetinblock)
658 struct vnode *dp;
659 register struct direct *ep;
660 int entryoffsetinblock;
661 {
662 register int i;
663 int namlen;
664
665 # if (BYTE_ORDER == LITTLE_ENDIAN)
666 if (dp->v_mount->mnt_maxsymlinklen > 0)
667 namlen = ep->d_namlen;
668 else
669 namlen = ep->d_type;
670 # else
671 namlen = ep->d_namlen;
672 # endif
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) {
676 /*return (1); */
677 printf("First bad\n");
678 goto bad;
679 }
680 if (ep->d_ino == 0)
681 return (0);
682 for (i = 0; i < namlen; i++)
683 if (ep->d_name[i] == '\0') {
684 /*return (1); */
685 printf("Second bad\n");
686 goto bad;
687 }
688 if (ep->d_name[i])
689 goto bad;
690 return (0);
691 bad:
692 return (1);
693 }
694
695 /*
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.
702 */
703 int
704 ufs_direnter(ip, dvp, cnp)
705 struct inode *ip;
706 struct vnode *dvp;
707 register struct componentname *cnp;
708 {
709 register struct inode *dp;
710 struct direct newdir;
711
712 #if DIAGNOSTIC
713 if ((cnp->cn_flags & SAVENAME) == 0)
714 panic("direnter: missing name");
715 #endif
716 dp = VTOI(dvp);
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);
722 else {
723 newdir.d_type = 0;
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; }
728 # endif
729 }
730 return (ufs_direnter2(dvp, &newdir, cnp->cn_cred, cnp->cn_proc));
731 }
732
733 /*
734 * Common entry point for directory entry removal used by ufs_direnter
735 * and ufs_whiteout
736 */
737 ufs_direnter2(dvp, dirp, cr, p)
738 struct vnode *dvp;
739 struct direct *dirp;
740 struct ucred *cr;
741 struct proc *p;
742 {
743 int newentrysize;
744 struct inode *dp;
745 struct buf *bp;
746 struct iovec aiov;
747 struct uio auio;
748 u_int dsize;
749 struct direct *ep, *nep;
750 int error, loc, spacefree;
751 char *dirbuf;
752 #if REV_ENDIAN_FS
753 struct mount *mp=dvp->v_mount;
754 int rev_endian=(mp->mnt_flag & MNT_REVEND);
755 #endif /* REV_ENDIAN_FS */
756
757 dp = VTOI(dvp);
758 newentrysize = DIRSIZ(FSFMT(dvp), dirp);
759
760 if (dp->i_count == 0) {
761 /*
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.
766 */
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;
775 auio.uio_iovcnt = 1;
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);
780 if (DIRBLKSIZ >
781 VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
782 /* XXX should grow with balloc() */
783 panic("ufs_direnter2: frag size");
784 else if (!error) {
785 dp->i_size = roundup(dp->i_size, DIRBLKSIZ);
786 dp->i_flag |= IN_CHANGE;
787 }
788 return (error);
789 }
790
791 /*
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.
798 */
799
800 /*
801 * Increase size of directory if entry eats into new space.
802 * This should never push the size past a new multiple of
803 * DIRBLKSIZE.
804 *
805 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
806 */
807 if (dp->i_offset + dp->i_count > dp->i_size)
808 dp->i_size = dp->i_offset + dp->i_count;
809 /*
810 * Get the block containing the space for the new directory entry.
811 */
812 if (error = VOP_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp))
813 return (error);
814 /*
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
819 * space.
820 */
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);
826 if (ep->d_ino) {
827 /* trim the existing slot */
828 ep->d_reclen = dsize;
829 ep = (struct direct *)((char *)ep + dsize);
830 } else {
831 /* overwrite; nothing there; header is ours */
832 spacefree += dsize;
833 }
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);
838 }
839 /*
840 * Update the pointer fields in the previous entry (if any),
841 * copy in the new entry, and write out the block.
842 */
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;
849 } else {
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);
855 }
856 bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize);
857 #if REV_ENDIAN_FS
858 if (rev_endian)
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);
865 return (error);
866 }
867
868 /*
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.
879 */
880 int
881 ufs_dirremove(dvp, cnp)
882 struct vnode *dvp;
883 struct componentname *cnp;
884 {
885 register struct inode *dp;
886 struct direct *ep;
887 struct buf *bp;
888 int error;
889 #if REV_ENDIAN_FS
890 struct mount *mp=dvp->v_mount;
891 int rev_endian=(mp->mnt_flag & MNT_REVEND);
892 #endif /* REV_ENDIAN_FS */
893
894 dp = VTOI(dvp);
895
896 if (cnp->cn_flags & DOWHITEOUT) {
897 /*
898 * Whiteout entry: set d_ino to WINO.
899 */
900 if (error =
901 VOP_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp))
902 return (error);
903 ep->d_ino = WINO;
904 ep->d_type = DT_WHT;
905 #if REV_ENDIAN_FS
906 if (rev_endian)
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;
911 return (error);
912 }
913
914 if (dp->i_count == 0) {
915 /*
916 * First entry in block: set d_ino to zero.
917 */
918 if (error =
919 VOP_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp))
920 return (error);
921 ep->d_ino = 0;
922 #if REV_ENDIAN_FS
923 if (rev_endian)
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;
928 return (error);
929 }
930 /*
931 * Collapse new free space into previous entry.
932 */
933 if (error = VOP_BLKATOFF(dvp, (off_t)(dp->i_offset - dp->i_count),
934 (char **)&ep, &bp))
935 return (error);
936 ep->d_reclen += dp->i_reclen;
937 #if REV_ENDIAN_FS
938 if (rev_endian)
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;
943 return (error);
944 }
945
946 /*
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.
950 */
951 int
952 ufs_dirrewrite(dp, ip, cnp)
953 struct inode *dp, *ip;
954 struct componentname *cnp;
955 {
956 struct buf *bp;
957 struct direct *ep;
958 struct vnode *vdp = ITOV(dp);
959 int error;
960
961 if (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp))
962 return (error);
963 ep->d_ino = ip->i_number;
964 if (vdp->v_mount->mnt_maxsymlinklen > 0)
965 ep->d_type = IFTODT(ip->i_mode);
966 #if REV_ENDIAN_FS
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;
972 return (error);
973 }
974
975 /*
976 * Check if a directory is empty or not.
977 * Inode supplied must be locked.
978 *
979 * Using a struct dirtemplate here is not precisely
980 * what we want, but better than using a struct direct.
981 *
982 * NB: does not handle corrupted directories.
983 */
984 int
985 ufs_dirempty(ip, parentino, cred)
986 register struct inode *ip;
987 ino_t parentino;
988 struct ucred *cred;
989 {
990 register off_t off;
991 struct dirtemplate dbuf;
992 register struct direct *dp = (struct direct *)&dbuf;
993 int error, count, namlen;
994 #if REV_ENDIAN_FS
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 */
999
1000 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1001
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);
1005 /*
1006 * Since we read MINDIRSIZ, residual must
1007 * be 0 unless we're at end of file.
1008 */
1009 if (error || count != 0)
1010 return (0);
1011 #if 0 /*REV_ENDIAN_FS */
1012 if (rev_endian)
1013 byte_swap_minidir_in(dp);
1014 #endif /* REV_ENDIAN_FS */
1015 /* avoid infinite loops */
1016 if (dp->d_reclen == 0)
1017 return (0);
1018 /* skip empty entries */
1019 if (dp->d_ino == 0 || dp->d_ino == WINO)
1020 continue;
1021 /* accept only "." and ".." */
1022 # if (BYTE_ORDER == LITTLE_ENDIAN)
1023 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0)
1024 namlen = dp->d_namlen;
1025 else
1026 namlen = dp->d_type;
1027 # else
1028 namlen = dp->d_namlen;
1029 # endif
1030 if (namlen > 2)
1031 return (0);
1032 if (dp->d_name[0] != '.')
1033 return (0);
1034 /*
1035 * At this point namlen must be 1 or 2.
1036 * 1 implies ".", 2 implies ".." if second
1037 * char is also "."
1038 */
1039 if (namlen == 1)
1040 continue;
1041 if (dp->d_name[1] == '.' && dp->d_ino == parentino)
1042 continue;
1043 return (0);
1044 }
1045 return (1);
1046 }
1047
1048 /*
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.
1052 */
1053 int
1054 ufs_checkpath(source, target, cred)
1055 struct inode *source, *target;
1056 struct ucred *cred;
1057 {
1058 struct vnode *vp;
1059 int error, rootino, namlen;
1060 struct dirtemplate dirbuf;
1061
1062 vp = ITOV(target);
1063 if (target->i_number == source->i_number) {
1064 error = EEXIST;
1065 goto out;
1066 }
1067 rootino = ROOTINO;
1068 error = 0;
1069 if (target->i_number == rootino)
1070 goto out;
1071
1072 for (;;) {
1073 if (vp->v_type != VDIR) {
1074 error = ENOTDIR;
1075 break;
1076 }
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);
1080 if (error != 0)
1081 break;
1082 # if (BYTE_ORDER == LITTLE_ENDIAN)
1083 if (vp->v_mount->mnt_maxsymlinklen > 0)
1084 namlen = dirbuf.dotdot_namlen;
1085 else
1086 namlen = dirbuf.dotdot_type;
1087 # else
1088 namlen = dirbuf.dotdot_namlen;
1089 # endif
1090 if (namlen != 2 ||
1091 dirbuf.dotdot_name[0] != '.' ||
1092 dirbuf.dotdot_name[1] != '.') {
1093 error = ENOTDIR;
1094 break;
1095 }
1096 if (dirbuf.dotdot_ino == source->i_number) {
1097 error = EINVAL;
1098 break;
1099 }
1100 if (dirbuf.dotdot_ino == rootino)
1101 break;
1102 vput(vp);
1103 if (error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino, &vp)) {
1104 vp = NULL;
1105 break;
1106 }
1107 }
1108
1109 out:
1110 if (error == ENOTDIR)
1111 printf("checkpath: .. not a directory\n");
1112 if (vp != NULL)
1113 vput(vp);
1114 return (error);
1115 }