]> git.saurik.com Git - apple/xnu.git/blob - bsd/ufs/ufs/ufs_lookup.c
6d48ca5af2c8271af4ac2d36971b6f7035ce74ae
[apple/xnu.git] / bsd / ufs / ufs / ufs_lookup.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
67 */
68 #include <rev_endian_fs.h>
69 #include <sys/param.h>
70 #include <sys/namei.h>
71 #include <sys/buf.h>
72 #include <sys/file.h>
73 #include <sys/mount_internal.h>
74 #include <sys/vnode_internal.h>
75 #include <sys/quota.h>
76 #include <sys/kauth.h>
77 #include <sys/uio_internal.h>
78
79 #include <ufs/ufs/quota.h>
80 #include <ufs/ufs/inode.h>
81 #include <ufs/ufs/dir.h>
82 #include <ufs/ufs/ufsmount.h>
83 #include <ufs/ufs/ufs_extern.h>
84 #include <ufs/ffs/ffs_extern.h>
85 #if REV_ENDIAN_FS
86 #include <ufs/ufs/ufs_byte_order.h>
87 #include <architecture/byte_order.h>
88 #endif /* REV_ENDIAN_FS */
89
90 extern struct nchstats nchstats;
91 #if DIAGNOSTIC
92 int dirchk = 1;
93 #else
94 int dirchk = 0;
95 #endif
96
97 #define FSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
98
99 /*
100 * Convert a component of a pathname into a pointer to a locked inode.
101 * This is a very central and rather complicated routine.
102 * If the file system is not maintained in a strict tree hierarchy,
103 * this can result in a deadlock situation (see comments in code below).
104 *
105 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
106 * on whether the name is to be looked up, created, renamed, or deleted.
107 * When CREATE, RENAME, or DELETE is specified, information usable in
108 * creating, renaming, or deleting a directory entry may be calculated.
109 * If flag has LOCKPARENT or'ed into it and the target of the pathname
110 * exists, lookup returns both the target and its parent directory locked.
111 * When creating or renaming and LOCKPARENT is specified, the target may
112 * not be ".". When deleting and LOCKPARENT is specified, the target may
113 * be ".".,
114 *
115 * Overall outline of ufs_lookup:
116 *
117 * check accessibility of directory
118 * look for name in cache, if found, then if at end of path
119 * and deleting or creating, drop it, else return name
120 * search for name in directory, to found or notfound
121 * notfound:
122 * if creating, return locked directory, leaving info on available slots
123 * else return error
124 * found:
125 * if at end of path and deleting, return information to allow delete
126 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
127 * inode and return info to allow rewrite
128 * if not at end, add name to cache; if at end and neither creating
129 * nor deleting, add name to cache
130 */
131 int
132 ufs_lookup(ap)
133 struct vnop_lookup_args /* {
134 struct vnode *a_dvp;
135 struct vnode **a_vpp;
136 struct componentname *a_cnp;
137 vfs_context_t a_context
138 } */ *ap;
139 {
140 register struct vnode *vdp; /* vnode for directory being searched */
141 register struct inode *dp; /* inode for directory being searched */
142 struct buf *bp; /* a buffer of directory entries */
143 register struct direct *ep; /* the current directory entry */
144 int entryoffsetinblock; /* offset of ep in bp's buffer */
145 enum {NONE, COMPACT, FOUND} slotstatus;
146 doff_t slotoffset; /* offset of area with free space */
147 int slotsize; /* size of area at slotoffset */
148 int slotfreespace; /* amount of space free in slot */
149 int slotneeded; /* size of the entry we're seeking */
150 int numdirpasses; /* strategy for directory search */
151 doff_t endsearch; /* offset to end directory search */
152 doff_t prevoff; /* prev entry dp->i_offset */
153 struct vnode *pdp; /* saved dp during symlink work */
154 struct vnode *tdp; /* returned by VFS_VGET */
155 doff_t enduseful; /* pointer past last used dir slot */
156 u_long bmask; /* block offset mask */
157 int wantparent; /* 1 => wantparent or lockparent flag */
158 int namlen, error;
159 struct vnode **vpp = ap->a_vpp;
160 struct componentname *cnp = ap->a_cnp;
161 int flags = cnp->cn_flags;
162 int nameiop = cnp->cn_nameiop;
163 vfs_context_t context = ap->a_context;
164 kauth_cred_t cred;
165 #if REV_ENDIAN_FS
166 int rev_endian=0;
167 #endif /* REV_ENDIAN_FS */
168
169
170 if (cnp->cn_namelen > MAXNAMLEN)
171 return (ENAMETOOLONG);
172
173 cred = vfs_context_ucred(context);
174 bp = NULL;
175 slotoffset = -1;
176 *vpp = NULL;
177 vdp = ap->a_dvp;
178 dp = VTOI(vdp);
179
180 wantparent = flags & (LOCKPARENT|WANTPARENT);
181
182 #if REV_ENDIAN_FS
183 rev_endian=(vdp->v_mount->mnt_flag & MNT_REVEND);
184 #endif /* REV_ENDIAN_FS */
185
186 /*
187 * Check accessiblity of directory.
188 */
189 if ((dp->i_mode & IFMT) != IFDIR)
190 return (ENOTDIR);
191
192 /*
193 * We now have a segment name to search for, and a directory to search.
194 *
195 * Before tediously performing a linear scan of the directory,
196 * check the name cache to see if the directory/name pair
197 * we are looking for is known already.
198 */
199 if (error = cache_lookup(vdp, vpp, cnp)) {
200 if (error == ENOENT)
201 return (error);
202 return (0);
203 }
204 /*
205 * Suppress search for slots unless creating
206 * file and at end of pathname, in which case
207 * we watch for a place to put the new file in
208 * case it doesn't already exist.
209 */
210 slotstatus = FOUND;
211 slotfreespace = slotsize = slotneeded = 0;
212 if ((nameiop == CREATE || nameiop == RENAME) &&
213 (flags & ISLASTCN)) {
214 slotstatus = NONE;
215 slotneeded = (sizeof(struct direct) - MAXNAMLEN +
216 cnp->cn_namelen + 3) &~ 3;
217 }
218 /*
219 * If there is cached information on a previous search of
220 * this directory, pick up where we last left off.
221 * We cache only lookups as these are the most common
222 * and have the greatest payoff. Caching CREATE has little
223 * benefit as it usually must search the entire directory
224 * to determine that the entry does not exist. Caching the
225 * location of the last DELETE or RENAME has not reduced
226 * profiling time and hence has been removed in the interest
227 * of simplicity.
228 */
229 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_vfsstat.f_iosize - 1;
230 if (nameiop != LOOKUP || dp->i_diroff == 0 ||
231 dp->i_diroff > dp->i_size) {
232 entryoffsetinblock = 0;
233 dp->i_offset = 0;
234 numdirpasses = 1;
235 } else {
236 dp->i_offset = dp->i_diroff;
237 if ((entryoffsetinblock = dp->i_offset & bmask) &&
238 (error = ffs_blkatoff(vdp, (off_t)dp->i_offset, NULL, &bp)))
239 goto out;
240 numdirpasses = 2;
241 nchstats.ncs_2passes++;
242 }
243 prevoff = dp->i_offset;
244 endsearch = roundup(dp->i_size, DIRBLKSIZ);
245 enduseful = 0;
246
247 searchloop:
248 while (dp->i_offset < endsearch) {
249 /*
250 * If necessary, get the next directory block.
251 */
252 if ((dp->i_offset & bmask) == 0) {
253 if (bp != NULL) {
254 #if REV_ENDIAN_FS
255 if (rev_endian)
256 byte_swap_dir_block_out(bp);
257 #endif /* REV_ENDIAN_FS */
258 buf_brelse(bp);
259 }
260 if (error = ffs_blkatoff(vdp, (off_t)dp->i_offset, NULL, &bp))
261 goto out;
262 entryoffsetinblock = 0;
263 }
264 /*
265 * If still looking for a slot, and at a DIRBLKSIZE
266 * boundary, have to start looking for free space again.
267 */
268 if (slotstatus == NONE &&
269 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
270 slotoffset = -1;
271 slotfreespace = 0;
272 }
273 /*
274 * Get pointer to next entry.
275 * Full validation checks are slow, but necessary.
276 */
277 ep = (struct direct *)((char *)buf_dataptr(bp) + entryoffsetinblock);
278 if (ep->d_reclen == 0 ||
279 ufs_dirbadentry(vdp, ep, entryoffsetinblock)) {
280 int i;
281
282 ufs_dirbad(dp, dp->i_offset, "mangled entry");
283 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
284 dp->i_offset += i;
285 entryoffsetinblock += i;
286 continue;
287 }
288
289 /*
290 * If an appropriate sized slot has not yet been found,
291 * check to see if one is available. Also accumulate space
292 * in the current block so that we can determine if
293 * compaction is viable.
294 */
295 if (slotstatus != FOUND) {
296 int size = ep->d_reclen;
297
298 if (ep->d_ino != 0)
299 size -= DIRSIZ(FSFMT(vdp), ep);
300 if (size > 0) {
301 if (size >= slotneeded) {
302 slotstatus = FOUND;
303 slotoffset = dp->i_offset;
304 slotsize = ep->d_reclen;
305 } else if (slotstatus == NONE) {
306 slotfreespace += size;
307 if (slotoffset == -1)
308 slotoffset = dp->i_offset;
309 if (slotfreespace >= slotneeded) {
310 slotstatus = COMPACT;
311 slotsize = dp->i_offset +
312 ep->d_reclen - slotoffset;
313 }
314 }
315 }
316 }
317
318 /*
319 * Check for a name match.
320 */
321 if (ep->d_ino) {
322 # if (BYTE_ORDER == LITTLE_ENDIAN)
323 if (vdp->v_mount->mnt_maxsymlinklen > 0)
324 namlen = ep->d_namlen;
325 else
326 namlen = ep->d_type;
327 # else
328 namlen = ep->d_namlen;
329 # endif
330 if (namlen == cnp->cn_namelen &&
331 !bcmp(cnp->cn_nameptr, ep->d_name,
332 (unsigned)namlen)) {
333 /*
334 * Save directory entry's inode number and
335 * reclen in ndp->ni_ufs area, and release
336 * directory buffer.
337 */
338 if (vdp->v_mount->mnt_maxsymlinklen > 0 &&
339 ep->d_type == DT_WHT) {
340 slotstatus = FOUND;
341 slotoffset = dp->i_offset;
342 slotsize = ep->d_reclen;
343 dp->i_reclen = slotsize;
344 enduseful = dp->i_size;
345 ap->a_cnp->cn_flags |= ISWHITEOUT;
346 numdirpasses--;
347 goto notfound;
348 }
349 dp->i_ino = ep->d_ino;
350 dp->i_reclen = ep->d_reclen;
351 #if REV_ENDIAN_FS
352 if (rev_endian)
353 byte_swap_dir_block_out(bp);
354 #endif /* REV_ENDIAN_FS */
355 buf_brelse(bp);
356 goto found;
357 }
358 }
359 prevoff = dp->i_offset;
360 dp->i_offset += ep->d_reclen;
361 entryoffsetinblock += ep->d_reclen;
362 if (ep->d_ino)
363 enduseful = dp->i_offset;
364 }
365 notfound:
366 /*
367 * If we started in the middle of the directory and failed
368 * to find our target, we must check the beginning as well.
369 */
370 if (numdirpasses == 2) {
371 numdirpasses--;
372 dp->i_offset = 0;
373 endsearch = dp->i_diroff;
374 goto searchloop;
375 }
376 if (bp != NULL) {
377 #if REV_ENDIAN_FS
378 if (rev_endian)
379 byte_swap_dir_block_out(bp);
380 #endif /* REV_ENDIAN_FS */
381 buf_brelse(bp);
382 }
383 /*
384 * If creating, and at end of pathname and current
385 * directory has not been removed, then can consider
386 * allowing file to be created.
387 */
388 if ((nameiop == CREATE || nameiop == RENAME ||
389 (nameiop == DELETE &&
390 (ap->a_cnp->cn_flags & DOWHITEOUT) &&
391 (ap->a_cnp->cn_flags & ISWHITEOUT))) &&
392 (flags & ISLASTCN) && dp->i_nlink != 0) {
393 /*
394 * Return an indication of where the new directory
395 * entry should be put. If we didn't find a slot,
396 * then set dp->i_count to 0 indicating
397 * that the new slot belongs at the end of the
398 * directory. If we found a slot, then the new entry
399 * can be put in the range from dp->i_offset to
400 * dp->i_offset + dp->i_count.
401 */
402 if (slotstatus == NONE) {
403 dp->i_offset = roundup(dp->i_size, DIRBLKSIZ);
404 dp->i_count = 0;
405 enduseful = dp->i_offset;
406 } else if (nameiop == DELETE) {
407 dp->i_offset = slotoffset;
408 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
409 dp->i_count = 0;
410 else
411 dp->i_count = dp->i_offset - prevoff;
412 } else {
413 dp->i_offset = slotoffset;
414 dp->i_count = slotsize;
415 if (enduseful < slotoffset + slotsize)
416 enduseful = slotoffset + slotsize;
417 }
418 dp->i_endoff = roundup(enduseful, DIRBLKSIZ);
419 dp->i_flag |= IN_CHANGE | IN_UPDATE;
420 /*
421 * We return with the directory locked, so that
422 * the parameters we set up above will still be
423 * valid if we actually decide to do a direnter().
424 * We return ni_vp == NULL to indicate that the entry
425 * does not currently exist; we leave a pointer to
426 * the (locked) directory inode in ndp->ni_dvp.
427 *
428 * NB - if the directory is unlocked, then this
429 * information cannot be used.
430 */
431 error = EJUSTRETURN;
432 goto out;
433 }
434 /*
435 * Insert name into cache (as non-existent) if appropriate.
436 */
437 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
438 cache_enter(vdp, *vpp, cnp);
439 error = ENOENT;
440 goto out;
441
442 found:
443 if (numdirpasses == 2)
444 nchstats.ncs_pass2++;
445 /*
446 * Check that directory length properly reflects presence
447 * of this entry.
448 */
449 if (entryoffsetinblock + DIRSIZ(FSFMT(vdp), ep) > dp->i_size) {
450 ufs_dirbad(dp, dp->i_offset, "i_size too small");
451 dp->i_size = entryoffsetinblock + DIRSIZ(FSFMT(vdp), ep);
452 dp->i_flag |= IN_CHANGE | IN_UPDATE;
453 }
454
455 /*
456 * Found component in pathname.
457 * If the final component of path name, save information
458 * in the cache as to where the entry was found.
459 */
460 if ((flags & ISLASTCN) && nameiop == LOOKUP)
461 dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
462
463 /*
464 * If deleting, and at end of pathname, return
465 * parameters which can be used to remove file.
466 * If the wantparent flag isn't set, we return only
467 * the directory (in ndp->ni_dvp), otherwise we go
468 * on and lock the inode, being careful with ".".
469 */
470 if (nameiop == DELETE && (flags & ISLASTCN)) {
471 /*
472 * Return pointer to current entry in dp->i_offset,
473 * and distance past previous entry (if there
474 * is a previous entry in this block) in dp->i_count.
475 * Save directory inode pointer in ndp->ni_dvp for dirremove().
476 */
477 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
478 dp->i_count = 0;
479 else
480 dp->i_count = dp->i_offset - prevoff;
481 if (dp->i_number == dp->i_ino) {
482 vnode_get(vdp);
483 *vpp = vdp;
484 error = 0;
485 goto out;
486 }
487 if (error = ffs_vget_internal(vdp->v_mount, dp->i_ino, &tdp, vdp, cnp, 0, 0))
488 goto out;
489 *vpp = tdp;
490 goto out;
491 }
492
493 /*
494 * If rewriting (RENAME), return the inode and the
495 * information required to rewrite the present directory
496 * Must get inode of directory entry to verify it's a
497 * regular file, or empty directory.
498 */
499 if (nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
500 /*
501 * Careful about locking second inode.
502 * This can only occur if the target is ".".
503 */
504 if (dp->i_number == dp->i_ino) {
505 error =EISDIR;
506 goto out;
507 }
508 if (error = ffs_vget_internal(vdp->v_mount, dp->i_ino, &tdp, vdp, cnp, 0, 0))
509 goto out;
510 *vpp = tdp;
511
512 goto out;
513 }
514
515 /*
516 * Step through the translation in the name. We do not `vnode_put' the
517 * directory because we may need it again if a symbolic link
518 * is relative to the current directory. Instead we save it
519 * unlocked as "pdp". We must get the target inode before unlocking
520 * the directory to insure that the inode will not be removed
521 * before we get it. We prevent deadlock by always fetching
522 * inodes from the root, moving down the directory tree. Thus
523 * when following backward pointers ".." we must unlock the
524 * parent directory before getting the requested directory.
525 * There is a potential race condition here if both the current
526 * and parent directories are removed before the VFS_VGET for the
527 * inode associated with ".." returns. We hope that this occurs
528 * infrequently since we cannot avoid this race condition without
529 * implementing a sophisticated deadlock detection algorithm.
530 * Note also that this simple deadlock detection scheme will not
531 * work if the file system has any hard links other than ".."
532 * that point backwards in the directory structure.
533 */
534 pdp = vdp;
535 if (flags & ISDOTDOT) {
536 if (error = ffs_vget_internal(vdp->v_mount, dp->i_ino, &tdp, vdp, cnp, 0, 0)) {
537 goto out;
538 }
539 *vpp = tdp;
540 } else if (dp->i_number == dp->i_ino) {
541 vnode_get(vdp); /* we want ourself, ie "." */
542 *vpp = vdp;
543 } else {
544 if (error = ffs_vget_internal(vdp->v_mount, dp->i_ino, &tdp, vdp, cnp, 0, 0))
545 goto out;
546 *vpp = tdp;
547 }
548
549 error = 0;
550 out:
551 return (error);
552 }
553
554 void
555 ufs_dirbad(ip, offset, how)
556 struct inode *ip;
557 doff_t offset;
558 const char *how;
559 {
560 struct mount *mp;
561
562 mp = ITOV(ip)->v_mount;
563 (void)printf("%s: bad dir ino %d at offset %d: %s\n",
564 mp->mnt_vfsstat.f_mntonname, ip->i_number, offset, how);
565 #if 0
566 if ((mp->mnt_vfsstat.f_flags & MNT_RDONLY) == 0)
567 panic("bad dir");
568 #endif
569 }
570
571 /*
572 * Do consistency checking on a directory entry:
573 * record length must be multiple of 4
574 * entry must fit in rest of its DIRBLKSIZ block
575 * record must be large enough to contain entry
576 * name is not longer than MAXNAMLEN
577 * name must be as long as advertised, and null terminated
578 */
579 int
580 ufs_dirbadentry(dp, ep, entryoffsetinblock)
581 struct vnode *dp;
582 register struct direct *ep;
583 int entryoffsetinblock;
584 {
585 register int i;
586 int namlen;
587 ino_t maxino = 0;
588 struct fs *fs;
589 struct ufsmount *ump = VFSTOUFS(dp->v_mount);
590
591 # if (BYTE_ORDER == LITTLE_ENDIAN)
592 if (dp->v_mount->mnt_maxsymlinklen > 0)
593 namlen = ep->d_namlen;
594 else
595 namlen = ep->d_type;
596 # else
597 namlen = ep->d_namlen;
598 # endif
599 if ((ep->d_reclen & 0x3) != 0 ||
600 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
601 ep->d_reclen < DIRSIZ(FSFMT(dp), ep) || namlen > MAXNAMLEN) {
602 /*return (1); */
603 printf("First bad\n");
604 goto bad;
605 }
606 if (ep->d_ino == 0)
607 return (0);
608 for (i = 0; i < namlen; i++)
609 if (ep->d_name[i] == '\0') {
610 /*return (1); */
611 printf("Second bad\n");
612 goto bad;
613 }
614 if (ep->d_name[i])
615 goto bad;
616
617 fs = ump->um_fs;
618 maxino = fs->fs_ncg * fs->fs_ipg;
619 if (ep->d_ino > maxino) {
620 printf("Third bad\n");
621 goto bad;
622 }
623
624 return (0);
625 bad:
626 return (1);
627 }
628
629 /*
630 * Write a directory entry after a call to namei, using the parameters
631 * that it left in nameidata. The argument ip is the inode which the new
632 * directory entry will refer to. Dvp is a pointer to the directory to
633 * be written, which was left locked by namei. Remaining parameters
634 * (dp->i_offset, dp->i_count) indicate how the space for the new
635 * entry is to be obtained.
636 */
637 int
638 ufs_direnter(ip, dvp, cnp)
639 struct inode *ip;
640 struct vnode *dvp;
641 register struct componentname *cnp;
642 {
643 register struct inode *dp;
644 struct direct newdir;
645
646 dp = VTOI(dvp);
647 newdir.d_ino = ip->i_number;
648 newdir.d_namlen = cnp->cn_namelen;
649 bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1);
650 if (dvp->v_mount->mnt_maxsymlinklen > 0)
651 newdir.d_type = IFTODT(ip->i_mode);
652 else {
653 newdir.d_type = 0;
654 # if (BYTE_ORDER == LITTLE_ENDIAN)
655 { u_char tmp = newdir.d_namlen;
656 newdir.d_namlen = newdir.d_type;
657 newdir.d_type = tmp; }
658 # endif
659 }
660 return (ufs_direnter2(dvp, &newdir, cnp->cn_context));
661 }
662
663 /*
664 * Common entry point for directory entry removal used by ufs_direnter
665 * and ufs_whiteout
666 */
667 int
668 ufs_direnter2(struct vnode *dvp, struct direct *dirp, vfs_context_t ctx)
669 {
670 int newentrysize;
671 struct inode *dp;
672 struct buf *bp;
673 uio_t auio;
674 u_int dsize;
675 struct direct *ep, *nep;
676 int error, loc, spacefree;
677 char *dirbuf;
678 char uio_buf[ UIO_SIZEOF(1) ];
679 #if REV_ENDIAN_FS
680 struct mount *mp=dvp->v_mount;
681 int rev_endian=(mp->mnt_flag & MNT_REVEND);
682 #endif /* REV_ENDIAN_FS */
683
684 dp = VTOI(dvp);
685 newentrysize = DIRSIZ(FSFMT(dvp), dirp);
686
687 if (dp->i_count == 0) {
688 /*
689 * If dp->i_count is 0, then namei could find no
690 * space in the directory. Here, dp->i_offset will
691 * be on a directory block boundary and we will write the
692 * new entry into a fresh block.
693 */
694 if (dp->i_offset & (DIRBLKSIZ - 1))
695 panic("ufs_direnter2: newblk");
696 dirp->d_reclen = DIRBLKSIZ;
697 auio = uio_createwithbuffer(1, dp->i_offset, UIO_SYSSPACE, UIO_WRITE,
698 &uio_buf[0], sizeof(uio_buf));
699 uio_addiov(auio, CAST_USER_ADDR_T(dirp), newentrysize);
700
701 error = ffs_write_internal(dvp, auio, IO_SYNC, vfs_context_ucred(ctx));
702 if (DIRBLKSIZ >
703 VFSTOUFS(dvp->v_mount)->um_mountp->mnt_vfsstat.f_bsize)
704 /* XXX should grow with balloc() */
705 panic("ufs_direnter2: frag size");
706 else if (!error) {
707 dp->i_size = roundup(dp->i_size, DIRBLKSIZ);
708 dp->i_flag |= IN_CHANGE;
709 }
710 return (error);
711 }
712
713 /*
714 * If dp->i_count is non-zero, then namei found space
715 * for the new entry in the range dp->i_offset to
716 * dp->i_offset + dp->i_count in the directory.
717 * To use this space, we may have to compact the entries located
718 * there, by copying them together towards the beginning of the
719 * block, leaving the free space in one usable chunk at the end.
720 */
721
722 /*
723 * Increase size of directory if entry eats into new space.
724 * This should never push the size past a new multiple of
725 * DIRBLKSIZE.
726 *
727 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
728 */
729 if (dp->i_offset + dp->i_count > dp->i_size)
730 dp->i_size = dp->i_offset + dp->i_count;
731 /*
732 * Get the block containing the space for the new directory entry.
733 */
734 if (error = ffs_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf, &bp))
735 return (error);
736 /*
737 * Find space for the new entry. In the simple case, the entry at
738 * offset base will have the space. If it does not, then namei
739 * arranged that compacting the region dp->i_offset to
740 * dp->i_offset + dp->i_count would yield the
741 * space.
742 */
743 ep = (struct direct *)dirbuf;
744 dsize = DIRSIZ(FSFMT(dvp), ep);
745 spacefree = ep->d_reclen - dsize;
746 for (loc = ep->d_reclen; loc < dp->i_count; ) {
747 nep = (struct direct *)(dirbuf + loc);
748 if (ep->d_ino) {
749 /* trim the existing slot */
750 ep->d_reclen = dsize;
751 ep = (struct direct *)((char *)ep + dsize);
752 } else {
753 /* overwrite; nothing there; header is ours */
754 spacefree += dsize;
755 }
756 dsize = DIRSIZ(FSFMT(dvp), nep);
757 spacefree += nep->d_reclen - dsize;
758 loc += nep->d_reclen;
759 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
760 }
761 /*
762 * Update the pointer fields in the previous entry (if any),
763 * copy in the new entry, and write out the block.
764 */
765 if (ep->d_ino == 0 ||
766 (ep->d_ino == WINO &&
767 bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
768 if (spacefree + dsize < newentrysize)
769 panic("ufs_direnter2: compact1");
770 dirp->d_reclen = spacefree + dsize;
771 } else {
772 if (spacefree < newentrysize)
773 panic("ufs_direnter2: compact2");
774 dirp->d_reclen = spacefree;
775 ep->d_reclen = dsize;
776 ep = (struct direct *)((char *)ep + dsize);
777 }
778 bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize);
779 #if REV_ENDIAN_FS
780 if (rev_endian)
781 byte_swap_dir_block_out(bp);
782 #endif /* REV_ENDIAN_FS */
783 if (mp->mnt_flag & MNT_ASYNC) {
784 error = 0;
785 buf_bdwrite(bp);
786 } else {
787 error = VNOP_BWRITE(bp);
788 }
789 dp->i_flag |= IN_CHANGE | IN_UPDATE;
790 if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
791 error = ffs_truncate_internal(dvp, (off_t)dp->i_endoff, IO_SYNC, vfs_context_ucred(ctx));
792
793 return (error);
794 }
795
796 /*
797 * Remove a directory entry after a call to namei, using
798 * the parameters which it left in nameidata. The entry
799 * dp->i_offset contains the offset into the directory of the
800 * entry to be eliminated. The dp->i_count field contains the
801 * size of the previous record in the directory. If this
802 * is 0, the first entry is being deleted, so we need only
803 * zero the inode number to mark the entry as free. If the
804 * entry is not the first in the directory, we must reclaim
805 * the space of the now empty record by adding the record size
806 * to the size of the previous entry.
807 */
808 int
809 ufs_dirremove(dvp, cnp)
810 struct vnode *dvp;
811 struct componentname *cnp;
812 {
813 register struct inode *dp;
814 struct direct *ep;
815 struct buf *bp;
816 int error;
817 #if REV_ENDIAN_FS
818 struct mount *mp=dvp->v_mount;
819 int rev_endian=(mp->mnt_flag & MNT_REVEND);
820 #endif /* REV_ENDIAN_FS */
821
822 dp = VTOI(dvp);
823
824 if (cnp->cn_flags & DOWHITEOUT) {
825 /*
826 * Whiteout entry: set d_ino to WINO.
827 */
828 if (error = ffs_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep, &bp))
829 return (error);
830 ep->d_ino = WINO;
831 ep->d_type = DT_WHT;
832 #if REV_ENDIAN_FS
833 if (rev_endian)
834 byte_swap_dir_block_out(bp);
835 #endif /* REV_ENDIAN_FS */
836 if (mp->mnt_flag & MNT_ASYNC) {
837 error = 0;
838 buf_bdwrite(bp);
839 } else {
840 error = VNOP_BWRITE(bp);
841 }
842 dp->i_flag |= IN_CHANGE | IN_UPDATE;
843 return (error);
844 }
845
846 if (dp->i_count == 0) {
847 /*
848 * First entry in block: set d_ino to zero.
849 */
850 if (error = ffs_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep, &bp))
851 return (error);
852 ep->d_ino = 0;
853 #if REV_ENDIAN_FS
854 if (rev_endian)
855 byte_swap_dir_block_out(bp);
856 #endif /* REV_ENDIAN_FS */
857 if (mp->mnt_flag & MNT_ASYNC) {
858 error = 0;
859 buf_bdwrite(bp);
860 } else {
861 error = VNOP_BWRITE(bp);
862 }
863 dp->i_flag |= IN_CHANGE | IN_UPDATE;
864 return (error);
865 }
866 /*
867 * Collapse new free space into previous entry.
868 */
869 if (error = ffs_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
870 (char **)&ep, &bp))
871 return (error);
872 ep->d_reclen += dp->i_reclen;
873 #if REV_ENDIAN_FS
874 if (rev_endian)
875 byte_swap_dir_block_out(bp);
876 #endif /* REV_ENDIAN_FS */
877 if (mp->mnt_flag & MNT_ASYNC) {
878 error = 0;
879 buf_bdwrite(bp);
880 } else {
881 error = VNOP_BWRITE(bp);
882 }
883 dp->i_flag |= IN_CHANGE | IN_UPDATE;
884
885 return (error);
886 }
887
888 /*
889 * Rewrite an existing directory entry to point at the inode
890 * supplied. The parameters describing the directory entry are
891 * set up by a call to namei.
892 */
893 int
894 ufs_dirrewrite(dp, ip, cnp)
895 struct inode *dp, *ip;
896 struct componentname *cnp;
897 {
898 struct buf *bp;
899 struct direct *ep;
900 struct vnode *vdp = ITOV(dp);
901 int error;
902
903 if (error = ffs_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep, &bp))
904 return (error);
905 ep->d_ino = ip->i_number;
906 if (vdp->v_mount->mnt_maxsymlinklen > 0)
907 ep->d_type = IFTODT(ip->i_mode);
908 #if REV_ENDIAN_FS
909 if (vdp->v_mount->mnt_flag & MNT_REVEND)
910 byte_swap_dir_block_out(bp);
911 #endif /* REV_ENDIAN_FS */
912 if (vdp->v_mount->mnt_flag & MNT_ASYNC) {
913 error = 0;
914 buf_bdwrite(bp);
915 } else {
916 error = VNOP_BWRITE(bp);
917 }
918 dp->i_flag |= IN_CHANGE | IN_UPDATE;
919 return (error);
920 }
921
922 /*
923 * Check if a directory is empty or not.
924 * Inode supplied must be locked.
925 *
926 * Using a struct dirtemplate here is not precisely
927 * what we want, but better than using a struct direct.
928 *
929 * NB: does not handle corrupted directories.
930 */
931 int
932 ufs_dirempty(struct inode *ip, ino_t parentino, kauth_cred_t cred)
933 {
934 register off_t off;
935 struct dirtemplate dbuf;
936 register struct direct *dp = (struct direct *)&dbuf;
937 int error, count, namlen;
938 #if REV_ENDIAN_FS
939 struct vnode *vp=ITOV(ip);
940 struct mount *mp=vp->v_mount;
941 int rev_endian=(mp->mnt_flag & MNT_REVEND);
942 #endif /* REV_ENDIAN_FS */
943
944 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
945
946 for (off = 0; off < ip->i_size; off += dp->d_reclen) {
947 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
948 UIO_SYSSPACE32, IO_NODELOCKED, cred, &count, (struct proc *)0);
949 /*
950 * Since we read MINDIRSIZ, residual must
951 * be 0 unless we're at end of file.
952 */
953 if (error || count != 0)
954 return (0);
955 #if 0 /*REV_ENDIAN_FS */
956 if (rev_endian)
957 byte_swap_minidir_in(dp);
958 #endif /* REV_ENDIAN_FS */
959 /* avoid infinite loops */
960 if (dp->d_reclen == 0)
961 return (0);
962 /* skip empty entries */
963 if (dp->d_ino == 0 || dp->d_ino == WINO)
964 continue;
965 /* accept only "." and ".." */
966 # if (BYTE_ORDER == LITTLE_ENDIAN)
967 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0)
968 namlen = dp->d_namlen;
969 else
970 namlen = dp->d_type;
971 # else
972 namlen = dp->d_namlen;
973 # endif
974 if (namlen > 2)
975 return (0);
976 if (dp->d_name[0] != '.')
977 return (0);
978 /*
979 * At this point namlen must be 1 or 2.
980 * 1 implies ".", 2 implies ".." if second
981 * char is also "."
982 */
983 if (namlen == 1)
984 continue;
985 if (dp->d_name[1] == '.' && dp->d_ino == parentino)
986 continue;
987 return (0);
988 }
989 return (1);
990 }
991
992 /*
993 * Check if source directory is in the path of the target directory.
994 * Target is supplied locked, source is unlocked.
995 */
996 int
997 ufs_checkpath(source, target, cred)
998 struct inode *source, *target;
999 kauth_cred_t cred;
1000 {
1001 struct vnode *vp;
1002 int error, rootino, namlen;
1003 int need_put = 0;
1004 struct dirtemplate dirbuf;
1005
1006 vp = ITOV(target);
1007 if (target->i_number == source->i_number) {
1008 error = EEXIST;
1009 goto out;
1010 }
1011 rootino = ROOTINO;
1012 error = 0;
1013 if (target->i_number == rootino)
1014 goto out;
1015
1016 for (;;) {
1017 if (vp->v_type != VDIR) {
1018 error = ENOTDIR;
1019 break;
1020 }
1021 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1022 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE32,
1023 IO_NODELOCKED, cred, (int *)0, (struct proc *)0);
1024 if (error != 0)
1025 break;
1026 # if (BYTE_ORDER == LITTLE_ENDIAN)
1027 if (vp->v_mount->mnt_maxsymlinklen > 0)
1028 namlen = dirbuf.dotdot_namlen;
1029 else
1030 namlen = dirbuf.dotdot_type;
1031 # else
1032 namlen = dirbuf.dotdot_namlen;
1033 # endif
1034 if (namlen != 2 ||
1035 dirbuf.dotdot_name[0] != '.' ||
1036 dirbuf.dotdot_name[1] != '.') {
1037 error = ENOTDIR;
1038 break;
1039 }
1040 if (dirbuf.dotdot_ino == source->i_number) {
1041 error = EINVAL;
1042 break;
1043 }
1044 if (dirbuf.dotdot_ino == rootino)
1045 break;
1046
1047 if (need_put)
1048 vnode_put(vp);
1049
1050 if (error = VFS_VGET(vp->v_mount, (ino64_t)dirbuf.dotdot_ino, &vp, NULL)) { /* XXX need context */
1051 vp = NULL;
1052 break;
1053 }
1054 need_put = 1;
1055 }
1056
1057 out:
1058 if (error == ENOTDIR)
1059 printf("checkpath: .. not a directory\n");
1060 if (need_put && vp)
1061 vnode_put(vp);
1062
1063 return (error);
1064 }