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