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