1 /* $OpenBSD: ufs_lookup.c,v 1.33 2005/07/20 16:30:35 pedro Exp $ */
2 /* $NetBSD: ufs_lookup.c,v 1.7 1996/02/09 22:36:06 christos Exp $ */
3
4 /*
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
7 * (c) UNIX System Laboratories, Inc.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)ufs_lookup.c 8.9 (Berkeley) 8/11/94
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/namei.h>
44 #include <sys/buf.h>
45 #include <sys/file.h>
46 #include <sys/stat.h>
47 #include <sys/mount.h>
48 #include <sys/vnode.h>
49
50 #include <uvm/uvm_extern.h>
51
52 #include <ufs/ufs/quota.h>
53 #include <ufs/ufs/inode.h>
54 #include <ufs/ufs/dir.h>
55 #ifdef UFS_DIRHASH
56 #include <ufs/ufs/dirhash.h>
57 #endif
58 #include <ufs/ufs/ufsmount.h>
59 #include <ufs/ufs/ufs_extern.h>
60
61 extern struct nchstats nchstats;
62
63 #ifdef DIAGNOSTIC
64 int dirchk = 1;
65 #else
66 int dirchk = 0;
67 #endif
68
69 #define FSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
70
71 /*
72 * Convert a component of a pathname into a pointer to a locked inode.
73 * This is a very central and rather complicated routine.
74 * If the file system is not maintained in a strict tree hierarchy,
75 * this can result in a deadlock situation (see comments in code below).
76 *
77 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
78 * on whether the name is to be looked up, created, renamed, or deleted.
79 * When CREATE, RENAME, or DELETE is specified, information usable in
80 * creating, renaming, or deleting a directory entry may be calculated.
81 * If flag has LOCKPARENT or'ed into it and the target of the pathname
82 * exists, lookup returns both the target and its parent directory locked.
83 * When creating or renaming and LOCKPARENT is specified, the target may
84 * not be ".". When deleting and LOCKPARENT is specified, the target may
85 * be "."., but the caller must check to ensure it does an vrele and vput
86 * instead of two vputs.
87 *
88 * Overall outline of ufs_lookup:
89 *
90 * check accessibility of directory
91 * look for name in cache, if found, then if at end of path
92 * and deleting or creating, drop it, else return name
93 * search for name in directory, to found or notfound
94 * notfound:
95 * if creating, return locked directory, leaving info on available slots
96 * else return error
97 * found:
98 * if at end of path and deleting, return information to allow delete
99 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
100 * inode and return info to allow rewrite
101 * if not at end, add name to cache; if at end and neither creating
102 * nor deleting, add name to cache
103 */
104 int
ufs_lookup(v)105 ufs_lookup(v)
106 void *v;
107 {
108 struct vop_lookup_args /* {
109 struct vnode *a_dvp;
110 struct vnode **a_vpp;
111 struct componentname *a_cnp;
112 } */ *ap = v;
113 register struct vnode *vdp; /* vnode for directory being searched */
114 register struct inode *dp; /* inode for directory being searched */
115 struct buf *bp; /* a buffer of directory entries */
116 register struct direct *ep; /* the current directory entry */
117 int entryoffsetinblock; /* offset of ep in bp's buffer */
118 enum {NONE, COMPACT, FOUND} slotstatus;
119 doff_t slotoffset; /* offset of area with free space */
120 int slotsize; /* size of area at slotoffset */
121 int slotfreespace; /* amount of space free in slot */
122 int slotneeded; /* size of the entry we're seeking */
123 int numdirpasses; /* strategy for directory search */
124 doff_t endsearch; /* offset to end directory search */
125 doff_t prevoff; /* prev entry dp->i_offset */
126 struct vnode *pdp; /* saved dp during symlink work */
127 struct vnode *tdp; /* returned by VFS_VGET */
128 doff_t enduseful; /* pointer past last used dir slot */
129 u_long bmask; /* block offset mask */
130 int lockparent; /* 1 => lockparent flag is set */
131 int wantparent; /* 1 => wantparent or lockparent flag */
132 int namlen, error;
133 struct vnode **vpp = ap->a_vpp;
134 struct componentname *cnp = ap->a_cnp;
135 struct ucred *cred = cnp->cn_cred;
136 int flags;
137 int nameiop = cnp->cn_nameiop;
138 struct proc *p = cnp->cn_proc;
139
140 cnp->cn_flags &= ~PDIRUNLOCK;
141 flags = cnp->cn_flags;
142
143 bp = NULL;
144 slotoffset = -1;
145 *vpp = NULL;
146 vdp = ap->a_dvp;
147 dp = VTOI(vdp);
148 lockparent = flags & LOCKPARENT;
149 wantparent = flags & (LOCKPARENT|WANTPARENT);
150
151 /*
152 * Check accessiblity of directory.
153 */
154 if ((dp->i_ffs_mode & IFMT) != IFDIR)
155 return (ENOTDIR);
156 if ((error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc)) != 0)
157 return (error);
158
159 if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
160 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
161 return (EROFS);
162
163 /*
164 * We now have a segment name to search for, and a directory to search.
165 *
166 * Before tediously performing a linear scan of the directory,
167 * check the name cache to see if the directory/name pair
168 * we are looking for is known already.
169 */
170 if ((error = cache_lookup(vdp, vpp, cnp)) >= 0)
171 return (error);
172
173 /*
174 * Suppress search for slots unless creating
175 * file and at end of pathname, in which case
176 * we watch for a place to put the new file in
177 * case it doesn't already exist.
178 */
179 slotstatus = FOUND;
180 slotfreespace = slotsize = slotneeded = 0;
181 if ((nameiop == CREATE || nameiop == RENAME) &&
182 (flags & ISLASTCN)) {
183 slotstatus = NONE;
184 slotneeded = (sizeof(struct direct) - MAXNAMLEN +
185 cnp->cn_namelen + 3) &~ 3;
186 }
187
188 /*
189 * If there is cached information on a previous search of
190 * this directory, pick up where we last left off.
191 * We cache only lookups as these are the most common
192 * and have the greatest payoff. Caching CREATE has little
193 * benefit as it usually must search the entire directory
194 * to determine that the entry does not exist. Caching the
195 * location of the last DELETE or RENAME has not reduced
196 * profiling time and hence has been removed in the interest
197 * of simplicity.
198 */
199 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
200
201 #ifdef UFS_DIRHASH
202 /*
203 * Use dirhash for fast operations on large directories. The logic
204 * to determine whether to hash the directory is contained within
205 * ufsdirhash_build(); a zero return means that it decided to hash
206 * this directory and it successfully built up the hash table.
207 */
208 if (ufsdirhash_build(dp) == 0) {
209 /* Look for a free slot if needed. */
210 enduseful = dp->i_size;
211 if (slotstatus != FOUND) {
212 slotoffset = ufsdirhash_findfree(dp, slotneeded,
213 &slotsize);
214 if (slotoffset >= 0) {
215 slotstatus = COMPACT;
216 enduseful = ufsdirhash_enduseful(dp);
217 if (enduseful < 0)
218 enduseful = dp->i_size;
219 }
220 }
221 /* Look up the component. */
222 numdirpasses = 1;
223 entryoffsetinblock = 0; /* silence compiler warning */
224 switch (ufsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
225 &dp->i_offset, &bp, nameiop == DELETE ? &prevoff : NULL)) {
226 case 0:
227 ep = (struct direct *)((char *)bp->b_data +
228 (dp->i_offset & bmask));
229 goto foundentry;
230 case ENOENT:
231 #define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
232 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
233 goto notfound;
234 default:
235 /* Something failed; just do a linear search. */
236 break;
237 }
238 }
239 #endif /* UFS_DIRHASH */
240
241 if (nameiop != LOOKUP || dp->i_diroff == 0 ||
242 dp->i_diroff >= dp->i_ffs_size) {
243 entryoffsetinblock = 0;
244 dp->i_offset = 0;
245 numdirpasses = 1;
246 } else {
247 dp->i_offset = dp->i_diroff;
248 if ((entryoffsetinblock = dp->i_offset & bmask) &&
249 (error = UFS_BUFATOFF(dp, (off_t)dp->i_offset, NULL, &bp)))
250 return (error);
251 numdirpasses = 2;
252 nchstats.ncs_2passes++;
253 }
254 prevoff = dp->i_offset;
255 endsearch = roundup(dp->i_ffs_size, DIRBLKSIZ);
256 enduseful = 0;
257
258 searchloop:
259 while (dp->i_offset < endsearch) {
260 /*
261 * If necessary, get the next directory block.
262 */
263 if ((dp->i_offset & bmask) == 0) {
264 if (bp != NULL)
265 brelse(bp);
266 error = UFS_BUFATOFF(dp, (off_t)dp->i_offset, NULL,
267 &bp);
268 if (error)
269 return (error);
270 entryoffsetinblock = 0;
271 }
272 /*
273 * If still looking for a slot, and at a DIRBLKSIZE
274 * boundary, have to start looking for free space again.
275 */
276 if (slotstatus == NONE &&
277 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
278 slotoffset = -1;
279 slotfreespace = 0;
280 }
281 /*
282 * Get pointer to next entry.
283 * Full validation checks are slow, so we only check
284 * enough to insure forward progress through the
285 * directory. Complete checks can be run by patching
286 * "dirchk" to be true.
287 */
288 ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
289 if (ep->d_reclen == 0 ||
290 (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) {
291 int i;
292
293 ufs_dirbad(dp, dp->i_offset, "mangled entry");
294 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
295 dp->i_offset += i;
296 entryoffsetinblock += i;
297 continue;
298 }
299
300 /*
301 * If an appropriate sized slot has not yet been found,
302 * check to see if one is available. Also accumulate space
303 * in the current block so that we can determine if
304 * compaction is viable.
305 */
306 if (slotstatus != FOUND) {
307 int size = ep->d_reclen;
308
309 if (ep->d_ino != 0)
310 size -= DIRSIZ(FSFMT(vdp), ep);
311 if (size > 0) {
312 if (size >= slotneeded) {
313 slotstatus = FOUND;
314 slotoffset = dp->i_offset;
315 slotsize = ep->d_reclen;
316 } else if (slotstatus == NONE) {
317 slotfreespace += size;
318 if (slotoffset == -1)
319 slotoffset = dp->i_offset;
320 if (slotfreespace >= slotneeded) {
321 slotstatus = COMPACT;
322 slotsize = dp->i_offset +
323 ep->d_reclen - slotoffset;
324 }
325 }
326 }
327 }
328
329 /*
330 * Check for a name match.
331 */
332 if (ep->d_ino) {
333 # if (BYTE_ORDER == LITTLE_ENDIAN)
334 if (vdp->v_mount->mnt_maxsymlinklen > 0)
335 namlen = ep->d_namlen;
336 else
337 namlen = ep->d_type;
338 # else
339 namlen = ep->d_namlen;
340 # endif
341 if (namlen == cnp->cn_namelen &&
342 !bcmp(cnp->cn_nameptr, ep->d_name,
343 (unsigned)namlen)) {
344 #ifdef UFS_DIRHASH
345 foundentry:
346 #endif
347 /*
348 * Save directory entry's inode number and
349 * reclen in ndp->ni_ufs area, and release
350 * directory buffer.
351 */
352 dp->i_ino = ep->d_ino;
353 dp->i_reclen = ep->d_reclen;
354 goto found;
355 }
356 }
357 prevoff = dp->i_offset;
358 dp->i_offset += ep->d_reclen;
359 entryoffsetinblock += ep->d_reclen;
360 if (ep->d_ino)
361 enduseful = dp->i_offset;
362 }
363 #ifdef UFS_DIRHASH
364 notfound:
365 #endif
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 brelse(bp);
378 /*
379 * If creating, and at end of pathname and current
380 * directory has not been removed, then can consider
381 * allowing file to be created.
382 */
383 if ((nameiop == CREATE || nameiop == RENAME) &&
384 (flags & ISLASTCN) && dp->i_effnlink != 0) {
385 /*
386 * Access for write is interpreted as allowing
387 * creation of files in the directory.
388 */
389 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc);
390 if (error)
391 return (error);
392 /*
393 * Return an indication of where the new directory
394 * entry should be put. If we didn't find a slot,
395 * then set dp->i_count to 0 indicating
396 * that the new slot belongs at the end of the
397 * directory. If we found a slot, then the new entry
398 * can be put in the range from dp->i_offset to
399 * dp->i_offset + dp->i_count.
400 */
401 if (slotstatus == NONE) {
402 dp->i_offset = roundup(dp->i_ffs_size, DIRBLKSIZ);
403 dp->i_count = 0;
404 enduseful = dp->i_offset;
405 } else if (nameiop == DELETE) {
406 dp->i_offset = slotoffset;
407 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
408 dp->i_count = 0;
409 else
410 dp->i_count = dp->i_offset - prevoff;
411 } else {
412 dp->i_offset = slotoffset;
413 dp->i_count = slotsize;
414 if (enduseful < slotoffset + slotsize)
415 enduseful = slotoffset + slotsize;
416 }
417 dp->i_endoff = roundup(enduseful, DIRBLKSIZ);
418 dp->i_flag |= IN_CHANGE | IN_UPDATE;
419 /*
420 * We return with the directory locked, so that
421 * the parameters we set up above will still be
422 * valid if we actually decide to do a direnter().
423 * We return ni_vp == NULL to indicate that the entry
424 * does not currently exist; we leave a pointer to
425 * the (locked) directory inode in ndp->ni_dvp.
426 * The pathname buffer is saved so that the name
427 * can be obtained later.
428 *
429 * NB - if the directory is unlocked, then this
430 * information cannot be used.
431 */
432 cnp->cn_flags |= SAVENAME;
433 if (!lockparent) {
434 VOP_UNLOCK(vdp, 0, p);
435 cnp->cn_flags |= PDIRUNLOCK;
436 }
437 return (EJUSTRETURN);
438 }
439 /*
440 * Insert name into cache (as non-existent) if appropriate.
441 */
442 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
443 cache_enter(vdp, *vpp, cnp);
444 return (ENOENT);
445
446 found:
447 if (numdirpasses == 2)
448 nchstats.ncs_pass2++;
449 /*
450 * Check that directory length properly reflects presence
451 * of this entry.
452 */
453 if (dp->i_offset + DIRSIZ(FSFMT(vdp), ep) > dp->i_ffs_size) {
454 ufs_dirbad(dp, dp->i_offset, "i_ffs_size too small");
455 dp->i_ffs_size = dp->i_offset + DIRSIZ(FSFMT(vdp), ep);
456 dp->i_flag |= IN_CHANGE | IN_UPDATE;
457 }
458 brelse(bp);
459
460 /*
461 * Found component in pathname.
462 * If the final component of path name, save information
463 * in the cache as to where the entry was found.
464 */
465 if ((flags & ISLASTCN) && nameiop == LOOKUP)
466 dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
467
468 /*
469 * If deleting, and at end of pathname, return
470 * parameters which can be used to remove file.
471 * If the wantparent flag isn't set, we return only
472 * the directory (in ndp->ni_dvp), otherwise we go
473 * on and lock the inode, being careful with ".".
474 */
475 if (nameiop == DELETE && (flags & ISLASTCN)) {
476 /*
477 * Write access to directory required to delete files.
478 */
479 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc);
480 if (error)
481 return (error);
482 /*
483 * Return pointer to current entry in dp->i_offset,
484 * and distance past previous entry (if there
485 * is a previous entry in this block) in dp->i_count.
486 * Save directory inode pointer in ndp->ni_dvp for dirremove().
487 */
488 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
489 dp->i_count = 0;
490 else
491 dp->i_count = dp->i_offset - prevoff;
492 if (dp->i_number == dp->i_ino) {
493 VREF(vdp);
494 *vpp = vdp;
495 return (0);
496 }
497 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
498 if (error)
499 return (error);
500 /*
501 * If directory is "sticky", then user must own
502 * the directory, or the file in it, else she
503 * may not delete it (unless she's root). This
504 * implements append-only directories.
505 */
506 if ((dp->i_ffs_mode & ISVTX) &&
507 cred->cr_uid != 0 &&
508 cred->cr_uid != dp->i_ffs_uid &&
509 VTOI(tdp)->i_ffs_uid != cred->cr_uid) {
510 vput(tdp);
511 return (EPERM);
512 }
513 *vpp = tdp;
514 if (!lockparent) {
515 VOP_UNLOCK(vdp, 0, p);
516 cnp->cn_flags |= PDIRUNLOCK;
517 }
518 return (0);
519 }
520
521 /*
522 * If rewriting (RENAME), return the inode and the
523 * information required to rewrite the present directory
524 * Must get inode of directory entry to verify it's a
525 * regular file, or empty directory.
526 */
527 if (nameiop == RENAME && wantparent &&
528 (flags & ISLASTCN)) {
529 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_proc);
530 if (error)
531 return (error);
532 /*
533 * Careful about locking second inode.
534 * This can only occur if the target is ".".
535 */
536 if (dp->i_number == dp->i_ino)
537 return (EISDIR);
538 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
539 if (error)
540 return (error);
541 *vpp = tdp;
542 cnp->cn_flags |= SAVENAME;
543 if (!lockparent) {
544 VOP_UNLOCK(vdp, 0, p);
545 cnp->cn_flags |= PDIRUNLOCK;
546 }
547 return (0);
548 }
549
550 /*
551 * Step through the translation in the name. We do not `vput' the
552 * directory because we may need it again if a symbolic link
553 * is relative to the current directory. Instead we save it
554 * unlocked as "pdp". We must get the target inode before unlocking
555 * the directory to insure that the inode will not be removed
556 * before we get it. We prevent deadlock by always fetching
557 * inodes from the root, moving down the directory tree. Thus
558 * when following backward pointers ".." we must unlock the
559 * parent directory before getting the requested directory.
560 * There is a potential race condition here if both the current
561 * and parent directories are removed before the VFS_VGET for the
562 * inode associated with ".." returns. We hope that this occurs
563 * infrequently since we cannot avoid this race condition without
564 * implementing a sophisticated deadlock detection algorithm.
565 * Note also that this simple deadlock detection scheme will not
566 * work if the file system has any hard links other than ".."
567 * that point backwards in the directory structure.
568 */
569 pdp = vdp;
570 if (flags & ISDOTDOT) {
571 VOP_UNLOCK(pdp, 0, p); /* race to get the inode */
572 cnp->cn_flags |= PDIRUNLOCK;
573 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
574 if (error) {
575 if (vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, p) == 0)
576 cnp->cn_flags &= ~PDIRUNLOCK;
577 return (error);
578 }
579 if (lockparent && (flags & ISLASTCN)) {
580 if ((error = vn_lock(pdp, LK_EXCLUSIVE, p))) {
581 vput(tdp);
582 return (error);
583 }
584 cnp->cn_flags &= ~PDIRUNLOCK;
585 }
586 *vpp = tdp;
587 } else if (dp->i_number == dp->i_ino) {
588 VREF(vdp); /* we want ourself, ie "." */
589 *vpp = vdp;
590 } else {
591 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
592 if (error)
593 return (error);
594 if (!lockparent || !(flags & ISLASTCN)) {
595 VOP_UNLOCK(pdp, 0, p);
596 cnp->cn_flags |= PDIRUNLOCK;
597 }
598 *vpp = tdp;
599 }
600
601 /*
602 * Insert name into cache if appropriate.
603 */
604 if (cnp->cn_flags & MAKEENTRY)
605 cache_enter(vdp, *vpp, cnp);
606 return (0);
607 }
608
609 void
ufs_dirbad(ip,offset,how)610 ufs_dirbad(ip, offset, how)
611 struct inode *ip;
612 doff_t offset;
613 char *how;
614 {
615 struct mount *mp;
616
617 mp = ITOV(ip)->v_mount;
618 (void)printf("%s: bad dir ino %d at offset %d: %s\n",
619 mp->mnt_stat.f_mntonname, ip->i_number, offset, how);
620 if ((mp->mnt_stat.f_flags & MNT_RDONLY) == 0)
621 panic("bad dir");
622 }
623
624 /*
625 * Do consistency checking on a directory entry:
626 * record length must be multiple of 4
627 * entry must fit in rest of its DIRBLKSIZ block
628 * record must be large enough to contain entry
629 * name is not longer than MAXNAMLEN
630 * name must be as long as advertised, and null terminated
631 */
632 int
ufs_dirbadentry(dp,ep,entryoffsetinblock)633 ufs_dirbadentry(dp, ep, entryoffsetinblock)
634 struct vnode *dp;
635 register struct direct *ep;
636 int entryoffsetinblock;
637 {
638 register int i;
639 int namlen;
640
641 # if (BYTE_ORDER == LITTLE_ENDIAN)
642 if (dp->v_mount->mnt_maxsymlinklen > 0)
643 namlen = ep->d_namlen;
644 else
645 namlen = ep->d_type;
646 # else
647 namlen = ep->d_namlen;
648 # endif
649 if ((ep->d_reclen & 0x3) != 0 ||
650 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
651 ep->d_reclen < DIRSIZ(FSFMT(dp), ep) || namlen > MAXNAMLEN) {
652 /*return (1); */
653 printf("First bad\n");
654 goto bad;
655 }
656 if (ep->d_ino == 0)
657 return (0);
658 for (i = 0; i < namlen; i++)
659 if (ep->d_name[i] == '\0') {
660 /*return (1); */
661 printf("Second bad\n");
662 goto bad;
663 }
664 if (ep->d_name[i])
665 goto bad;
666 return (0);
667 bad:
668 return (1);
669 }
670
671 /*
672 * Construct a new directory entry after a call to namei, using the
673 * parameters that it left in the componentname argument cnp. The
674 * argument ip is the inode to which the new directory entry will refer.
675 */
676 void
ufs_makedirentry(ip,cnp,newdirp)677 ufs_makedirentry(ip, cnp, newdirp)
678 struct inode *ip;
679 struct componentname *cnp;
680 struct direct *newdirp;
681 {
682
683 #ifdef DIAGNOSTIC
684 if ((cnp->cn_flags & SAVENAME) == 0)
685 panic("ufs_makedirentry: missing name");
686 #endif
687 newdirp->d_ino = ip->i_number;
688 newdirp->d_namlen = cnp->cn_namelen;
689 bcopy(cnp->cn_nameptr, newdirp->d_name, (unsigned)cnp->cn_namelen + 1);
690 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0)
691 newdirp->d_type = IFTODT(ip->i_ffs_mode);
692 else {
693 newdirp->d_type = 0;
694 # if (BYTE_ORDER == LITTLE_ENDIAN)
695 { u_char tmp = newdirp->d_namlen;
696 newdirp->d_namlen = newdirp->d_type;
697 newdirp->d_type = tmp; }
698 # endif
699 }
700 }
701
702 /*
703 * Write a directory entry after a call to namei, using the parameters
704 * that it left in nameidata. The argument dirp is the new directory
705 * entry contents. Dvp is a pointer to the directory to be written,
706 * which was left locked by namei. Remaining parameters (dp->i_offset,
707 * dp->i_count) indicate how the space for the new entry is to be obtained.
708 * Non-null bp indicates that a directory is being created (for the
709 * soft dependency code).
710 */
711 int
ufs_direnter(dvp,tvp,dirp,cnp,newdirbp)712 ufs_direnter(dvp, tvp, dirp, cnp, newdirbp)
713 struct vnode *dvp;
714 struct vnode *tvp;
715 struct direct *dirp;
716 struct componentname *cnp;
717 struct buf *newdirbp;
718 {
719 struct ucred *cr;
720 struct proc *p;
721 int newentrysize;
722 struct inode *dp;
723 struct buf *bp;
724 u_int dsize;
725 struct direct *ep, *nep;
726 int error, ret, blkoff, loc, spacefree, flags;
727 char *dirbuf;
728
729 error = 0;
730 cr = cnp->cn_cred;
731 p = cnp->cn_proc;
732 dp = VTOI(dvp);
733 newentrysize = DIRSIZ(FSFMT(dvp), dirp);
734
735 if (dp->i_count == 0) {
736 /*
737 * If dp->i_count is 0, then namei could find no
738 * space in the directory. Here, dp->i_offset will
739 * be on a directory block boundary and we will write the
740 * new entry into a fresh block.
741 */
742 if (dp->i_offset & (DIRBLKSIZ - 1))
743 panic("ufs_direnter: newblk");
744 flags = B_CLRBUF;
745 if (!DOINGSOFTDEP(dvp))
746 flags |= B_SYNC;
747 if ((error = UFS_BUF_ALLOC(dp, (off_t)dp->i_offset, DIRBLKSIZ,
748 cr, flags, &bp)) != 0) {
749 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
750 bdwrite(newdirbp);
751 return (error);
752 }
753 dp->i_ffs_size = dp->i_offset + DIRBLKSIZ;
754 dp->i_flag |= IN_CHANGE | IN_UPDATE;
755 uvm_vnp_setsize(dvp, dp->i_ffs_size);
756 dirp->d_reclen = DIRBLKSIZ;
757 blkoff = dp->i_offset &
758 (VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_iosize - 1);
759 bcopy((caddr_t)dirp, (caddr_t)bp->b_data + blkoff,newentrysize);
760
761 #ifdef UFS_DIRHASH
762 if (dp->i_dirhash != NULL) {
763 ufsdirhash_newblk(dp, dp->i_offset);
764 ufsdirhash_add(dp, dirp, dp->i_offset);
765 ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff,
766 dp->i_offset);
767 }
768 #endif
769
770 if (DOINGSOFTDEP(dvp)) {
771 /*
772 * Ensure that the entire newly allocated block is a
773 * valid directory so that future growth within the
774 * block does not have to ensure that the block is
775 * written before the inode.
776 */
777 blkoff += DIRBLKSIZ;
778 while (blkoff < bp->b_bcount) {
779 ((struct direct *)
780 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
781 blkoff += DIRBLKSIZ;
782 }
783 if (softdep_setup_directory_add(bp, dp, dp->i_offset,
784 dirp->d_ino, newdirbp, 1) == 0) {
785 bdwrite(bp);
786 return (UFS_UPDATE(dp, 0));
787 }
788 /* We have just allocated a directory block in an
789 * indirect block. Rather than tracking when it gets
790 * claimed by the inode, we simply do a VOP_FSYNC
791 * now to ensure that it is there (in case the user
792 * does a future fsync). Note that we have to unlock
793 * the inode for the entry that we just entered, as
794 * the VOP_FSYNC may need to lock other inodes which
795 * can lead to deadlock if we also hold a lock on
796 * the newly entered node.
797 */
798 if ((error = VOP_BWRITE(bp)))
799 return (error);
800 if (tvp != NULL)
801 VOP_UNLOCK(tvp, 0, p);
802 error = VOP_FSYNC(dvp, p->p_ucred, MNT_WAIT, p);
803 if (tvp != NULL)
804 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, p);
805 return (error);
806 }
807 error = VOP_BWRITE(bp);
808 ret = UFS_UPDATE(dp, !DOINGSOFTDEP(dvp));
809 if (error == 0)
810 return (ret);
811 return (error);
812 }
813
814 /*
815 * If dp->i_count is non-zero, then namei found space for the new
816 * entry in the range dp->i_offset to dp->i_offset + dp->i_count
817 * in the directory. To use this space, we may have to compact
818 * the entries located there, by copying them together towards the
819 * beginning of the block, leaving the free space in one usable
820 * chunk at the end.
821 */
822
823 /*
824 * Increase size of directory if entry eats into new space.
825 * This should never push the size past a new multiple of
826 * DIRBLKSIZE.
827 *
828 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
829 */
830 if (dp->i_offset + dp->i_count > dp->i_ffs_size)
831 dp->i_ffs_size = dp->i_offset + dp->i_count;
832 /*
833 * Get the block containing the space for the new directory entry.
834 */
835 if ((error = UFS_BUFATOFF(dp, (off_t)dp->i_offset, &dirbuf, &bp))
836 != 0) {
837 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
838 bdwrite(newdirbp);
839 return (error);
840 }
841 /*
842 * Find space for the new entry. In the simple case, the entry at
843 * offset base will have the space. If it does not, then namei
844 * arranged that compacting the region dp->i_offset to
845 * dp->i_offset + dp->i_count would yield the space.
846 */
847 ep = (struct direct *)dirbuf;
848 dsize = ep->d_ino ? DIRSIZ(FSFMT(dvp), ep) : 0;
849 spacefree = ep->d_reclen - dsize;
850 for (loc = ep->d_reclen; loc < dp->i_count; ) {
851 nep = (struct direct *)(dirbuf + loc);
852
853 /* Trim the existing slot (NB: dsize may be zero). */
854 ep->d_reclen = dsize;
855 ep = (struct direct *)((char *)ep + dsize);
856
857 /* Read nep->d_reclen now as the bcopy() may clobber it. */
858 loc += nep->d_reclen;
859 if (nep->d_ino == 0) {
860 /*
861 * A mid-block unused entry. Such entries are
862 * never created by the kernel, but fsck_ffs
863 * can create them (and it doesn't fix them).
864 *
865 * Add up the free space, and initialise the
866 * relocated entry since we don't bcopy it.
867 */
868 spacefree += nep->d_reclen;
869 ep->d_ino = 0;
870 dsize = 0;
871 continue;
872 }
873 dsize = DIRSIZ(FSFMT(dvp), nep);
874 spacefree += nep->d_reclen - dsize;
875 #ifdef UFS_DIRHASH
876 if (dp->i_dirhash != NULL)
877 ufsdirhash_move(dp, nep,
878 dp->i_offset + ((char *)nep - dirbuf),
879 dp->i_offset + ((char *)ep - dirbuf));
880 #endif
881 if (DOINGSOFTDEP(dvp))
882 softdep_change_directoryentry_offset(dp, dirbuf,
883 (caddr_t)nep, (caddr_t)ep, dsize);
884 else
885 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
886 }
887 /*
888 * Here, `ep' points to a directory entry containing `dsize' in-use
889 * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
890 * then the entry is completely unused (dsize == 0). The value
891 * of ep->d_reclen is always indeterminate.
892 *
893 * Update the pointer fields in the previous entry (if any),
894 * copy in the new entry, and write out the block.
895 */
896 if (ep->d_ino == 0) {
897 if (spacefree + dsize < newentrysize)
898 panic("ufs_direnter: compact1");
899 dirp->d_reclen = spacefree + dsize;
900 } else {
901 if (spacefree < newentrysize)
902 panic("ufs_direnter: compact2");
903 dirp->d_reclen = spacefree;
904 ep->d_reclen = dsize;
905 ep = (struct direct *)((char *)ep + dsize);
906 }
907
908 #ifdef UFS_DIRHASH
909 if (dp->i_dirhash != NULL && (ep->d_ino == 0 ||
910 dirp->d_reclen == spacefree))
911 ufsdirhash_add(dp, dirp, dp->i_offset + ((char *)ep - dirbuf));
912 #endif
913 bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize);
914 #ifdef UFS_DIRHASH
915 if (dp->i_dirhash != NULL)
916 ufsdirhash_checkblock(dp, dirbuf -
917 (dp->i_offset & (DIRBLKSIZ - 1)),
918 dp->i_offset & ~(DIRBLKSIZ - 1));
919 #endif
920
921 if (DOINGSOFTDEP(dvp)) {
922 (void)softdep_setup_directory_add(bp, dp,
923 dp->i_offset + (caddr_t)ep - dirbuf,
924 dirp->d_ino, newdirbp, 0);
925 bdwrite(bp);
926 } else {
927 error = VOP_BWRITE(bp);
928 }
929 dp->i_flag |= IN_CHANGE | IN_UPDATE;
930
931 /*
932 * If all went well, and the directory can be shortened, proceed
933 * with the truncation. Note that we have to unlock the inode for
934 * the entry that we just entered, as the truncation may need to
935 * lock other inodes which can lead to deadlock if we also hold a
936 * lock on the newly entered node.
937 */
938
939 if (error == 0 && dp->i_endoff && dp->i_endoff < dp->i_ffs_size) {
940 if (tvp != NULL)
941 VOP_UNLOCK(tvp, 0, p);
942 #ifdef UFS_DIRHASH
943 if (dp->i_dirhash != NULL)
944 ufsdirhash_dirtrunc(dp, dp->i_endoff);
945 #endif
946
947
948 error = UFS_TRUNCATE(dp, (off_t)dp->i_endoff, IO_SYNC, cr);
949
950 if (tvp != NULL)
951 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, p);
952 }
953 return (error);
954 }
955
956 /*
957 * Remove a directory entry after a call to namei, using
958 * the parameters which it left in nameidata. The entry
959 * dp->i_offset contains the offset into the directory of the
960 * entry to be eliminated. The dp->i_count field contains the
961 * size of the previous record in the directory. If this
962 * is 0, the first entry is being deleted, so we need only
963 * zero the inode number to mark the entry as free. If the
964 * entry is not the first in the directory, we must reclaim
965 * the space of the now empty record by adding the record size
966 * to the size of the previous entry.
967 */
968 int
ufs_dirremove(dvp,ip,flags,isrmdir)969 ufs_dirremove(dvp, ip, flags, isrmdir)
970 struct vnode *dvp;
971 struct inode *ip;
972 int flags;
973 int isrmdir;
974 {
975 struct inode *dp;
976 struct direct *ep;
977 struct buf *bp;
978 int error;
979
980 dp = VTOI(dvp);
981
982 if ((error = UFS_BUFATOFF(dp,
983 (off_t)(dp->i_offset - dp->i_count), (char **)&ep, &bp)) != 0)
984 return (error);
985 #ifdef UFS_DIRHASH
986 /*
987 * Remove the dirhash entry. This is complicated by the fact
988 * that `ep' is the previous entry when dp->i_count != 0.
989 */
990 if (dp->i_dirhash != NULL)
991 ufsdirhash_remove(dp, (dp->i_count == 0) ? ep :
992 (struct direct *)((char *)ep + ep->d_reclen), dp->i_offset);
993 #endif
994
995 if (dp->i_count == 0) {
996 /*
997 * First entry in block: set d_ino to zero.
998 */
999 ep->d_ino = 0;
1000 } else {
1001 /*
1002 * Collapse new free space into previous entry.
1003 */
1004 ep->d_reclen += dp->i_reclen;
1005 }
1006 #ifdef UFS_DIRHASH
1007 if (dp->i_dirhash != NULL)
1008 ufsdirhash_checkblock(dp, (char *)ep -
1009 ((dp->i_offset - dp->i_count) & (DIRBLKSIZ - 1)),
1010 dp->i_offset & ~(DIRBLKSIZ - 1));
1011 #endif
1012 if (DOINGSOFTDEP(dvp)) {
1013 if (ip) {
1014 ip->i_effnlink--;
1015 softdep_change_linkcnt(ip, 0);
1016 softdep_setup_remove(bp, dp, ip, isrmdir);
1017 }
1018 if (softdep_slowdown(dvp)) {
1019 error = bwrite(bp);
1020 } else {
1021 bdwrite(bp);
1022 error = 0;
1023 }
1024 } else {
1025 if (ip) {
1026 ip->i_effnlink--;
1027 ip->i_ffs_nlink--;
1028 ip->i_flag |= IN_CHANGE;
1029 }
1030 if (DOINGASYNC(dvp) && dp->i_count != 0) {
1031 bdwrite(bp);
1032 error = 0;
1033 } else
1034 error = bwrite(bp);
1035 }
1036 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1037 return (error);
1038 }
1039
1040 /*
1041 * Rewrite an existing directory entry to point at the inode
1042 * supplied. The parameters describing the directory entry are
1043 * set up by a call to namei.
1044 */
1045 int
ufs_dirrewrite(dp,oip,newinum,newtype,isrmdir)1046 ufs_dirrewrite(dp, oip, newinum, newtype, isrmdir)
1047 struct inode *dp, *oip;
1048 ino_t newinum;
1049 int newtype;
1050 int isrmdir;
1051 {
1052 struct buf *bp;
1053 struct direct *ep;
1054 struct vnode *vdp = ITOV(dp);
1055 int error;
1056
1057 error = UFS_BUFATOFF(dp, (off_t)dp->i_offset, (char **)&ep, &bp);
1058 if (error)
1059 return (error);
1060 ep->d_ino = newinum;
1061 if (vdp->v_mount->mnt_maxsymlinklen > 0)
1062 ep->d_type = newtype;
1063 oip->i_effnlink--;
1064 if (DOINGSOFTDEP(vdp)) {
1065 softdep_change_linkcnt(oip, 0);
1066 softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir);
1067 bdwrite(bp);
1068 } else {
1069 oip->i_ffs_nlink--;
1070 oip->i_flag |= IN_CHANGE;
1071 if (DOINGASYNC(vdp)) {
1072 bdwrite(bp);
1073 error = 0;
1074 } else {
1075 error = VOP_BWRITE(bp);
1076 }
1077 }
1078 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1079 return (error);
1080 }
1081
1082 /*
1083 * Check if a directory is empty or not.
1084 * Inode supplied must be locked.
1085 *
1086 * Using a struct dirtemplate here is not precisely
1087 * what we want, but better than using a struct direct.
1088 *
1089 * NB: does not handle corrupted directories.
1090 */
1091 int
ufs_dirempty(ip,parentino,cred)1092 ufs_dirempty(ip, parentino, cred)
1093 struct inode *ip;
1094 ino_t parentino;
1095 struct ucred *cred;
1096 {
1097 off_t off, m;
1098 struct dirtemplate dbuf;
1099 struct direct *dp = (struct direct *)&dbuf;
1100 int error, namlen;
1101 size_t count;
1102 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1103
1104 m = ip->i_ffs_size;
1105 for (off = 0; off < m; off += dp->d_reclen) {
1106 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
1107 UIO_SYSSPACE, IO_NODELOCKED, cred, &count, (struct proc *)0);
1108 /*
1109 * Since we read MINDIRSIZ, residual must
1110 * be 0 unless we're at end of file.
1111 */
1112 if (error || count != 0)
1113 return (0);
1114 /* avoid infinite loops */
1115 if (dp->d_reclen == 0)
1116 return (0);
1117 /* skip empty entries */
1118 if (dp->d_ino == 0)
1119 continue;
1120 /* accept only "." and ".." */
1121 # if (BYTE_ORDER == LITTLE_ENDIAN)
1122 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0)
1123 namlen = dp->d_namlen;
1124 else
1125 namlen = dp->d_type;
1126 # else
1127 namlen = dp->d_namlen;
1128 # endif
1129 if (namlen > 2)
1130 return (0);
1131 if (dp->d_name[0] != '.')
1132 return (0);
1133 /*
1134 * At this point namlen must be 1 or 2.
1135 * 1 implies ".", 2 implies ".." if second
1136 * char is also "."
1137 */
1138 if (namlen == 1 && dp->d_ino == ip->i_number)
1139 continue;
1140 if (dp->d_name[1] == '.' && dp->d_ino == parentino)
1141 continue;
1142 return (0);
1143 }
1144 return (1);
1145 }
1146
1147 /*
1148 * Check if source directory is in the path of the target directory.
1149 * Target is supplied locked, source is unlocked.
1150 * The target is always vput before returning.
1151 */
1152 int
ufs_checkpath(source,target,cred)1153 ufs_checkpath(source, target, cred)
1154 struct inode *source, *target;
1155 struct ucred *cred;
1156 {
1157 struct vnode *vp;
1158 int error, rootino, namlen;
1159 struct dirtemplate dirbuf;
1160
1161 vp = ITOV(target);
1162 if (target->i_number == source->i_number) {
1163 error = EEXIST;
1164 goto out;
1165 }
1166 rootino = ROOTINO;
1167 error = 0;
1168 if (target->i_number == rootino)
1169 goto out;
1170
1171 for (;;) {
1172 if (vp->v_type != VDIR) {
1173 error = ENOTDIR;
1174 break;
1175 }
1176 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1177 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1178 IO_NODELOCKED, cred, NULL, (struct proc *)0);
1179 if (error != 0)
1180 break;
1181 # if (BYTE_ORDER == LITTLE_ENDIAN)
1182 if (vp->v_mount->mnt_maxsymlinklen > 0)
1183 namlen = dirbuf.dotdot_namlen;
1184 else
1185 namlen = dirbuf.dotdot_type;
1186 # else
1187 namlen = dirbuf.dotdot_namlen;
1188 # endif
1189 if (namlen != 2 ||
1190 dirbuf.dotdot_name[0] != '.' ||
1191 dirbuf.dotdot_name[1] != '.') {
1192 error = ENOTDIR;
1193 break;
1194 }
1195 if (dirbuf.dotdot_ino == source->i_number) {
1196 error = EINVAL;
1197 break;
1198 }
1199 if (dirbuf.dotdot_ino == rootino)
1200 break;
1201 vput(vp);
1202 error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino, &vp);
1203 if (error) {
1204 vp = NULL;
1205 break;
1206 }
1207 }
1208
1209 out:
1210 if (error == ENOTDIR)
1211 printf("checkpath: .. not a directory\n");
1212 if (vp != NULL)
1213 vput(vp);
1214 return (error);
1215 }
1216